1 //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Expr nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGCall.h"
17 #include "CGCXXABI.h"
18 #include "CGDebugInfo.h"
19 #include "CGRecordLayout.h"
20 #include "CGObjCRuntime.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/Frontend/CodeGenOptions.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/Target/TargetData.h"
26 using namespace clang;
27 using namespace CodeGen;
28
29 //===--------------------------------------------------------------------===//
30 // Miscellaneous Helper Methods
31 //===--------------------------------------------------------------------===//
32
EmitCastToVoidPtr(llvm::Value * value)33 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
34 unsigned addressSpace =
35 cast<llvm::PointerType>(value->getType())->getAddressSpace();
36
37 llvm::PointerType *destType = Int8PtrTy;
38 if (addressSpace)
39 destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
40
41 if (value->getType() == destType) return value;
42 return Builder.CreateBitCast(value, destType);
43 }
44
45 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
46 /// block.
CreateTempAlloca(llvm::Type * Ty,const llvm::Twine & Name)47 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
48 const llvm::Twine &Name) {
49 if (!Builder.isNamePreserving())
50 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
51 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
52 }
53
InitTempAlloca(llvm::AllocaInst * Var,llvm::Value * Init)54 void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
55 llvm::Value *Init) {
56 llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
57 llvm::BasicBlock *Block = AllocaInsertPt->getParent();
58 Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
59 }
60
CreateIRTemp(QualType Ty,const llvm::Twine & Name)61 llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
62 const llvm::Twine &Name) {
63 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
64 // FIXME: Should we prefer the preferred type alignment here?
65 CharUnits Align = getContext().getTypeAlignInChars(Ty);
66 Alloc->setAlignment(Align.getQuantity());
67 return Alloc;
68 }
69
CreateMemTemp(QualType Ty,const llvm::Twine & Name)70 llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
71 const llvm::Twine &Name) {
72 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
73 // FIXME: Should we prefer the preferred type alignment here?
74 CharUnits Align = getContext().getTypeAlignInChars(Ty);
75 Alloc->setAlignment(Align.getQuantity());
76 return Alloc;
77 }
78
79 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
80 /// expression and compare the result against zero, returning an Int1Ty value.
EvaluateExprAsBool(const Expr * E)81 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
82 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
83 llvm::Value *MemPtr = EmitScalarExpr(E);
84 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
85 }
86
87 QualType BoolTy = getContext().BoolTy;
88 if (!E->getType()->isAnyComplexType())
89 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
90
91 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
92 }
93
94 /// EmitIgnoredExpr - Emit code to compute the specified expression,
95 /// ignoring the result.
EmitIgnoredExpr(const Expr * E)96 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
97 if (E->isRValue())
98 return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
99
100 // Just emit it as an l-value and drop the result.
101 EmitLValue(E);
102 }
103
104 /// EmitAnyExpr - Emit code to compute the specified expression which
105 /// can have any type. The result is returned as an RValue struct.
106 /// If this is an aggregate expression, AggSlot indicates where the
107 /// result should be returned.
EmitAnyExpr(const Expr * E,AggValueSlot AggSlot,bool IgnoreResult)108 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, AggValueSlot AggSlot,
109 bool IgnoreResult) {
110 if (!hasAggregateLLVMType(E->getType()))
111 return RValue::get(EmitScalarExpr(E, IgnoreResult));
112 else if (E->getType()->isAnyComplexType())
113 return RValue::getComplex(EmitComplexExpr(E, IgnoreResult, IgnoreResult));
114
115 EmitAggExpr(E, AggSlot, IgnoreResult);
116 return AggSlot.asRValue();
117 }
118
119 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
120 /// always be accessible even if no aggregate location is provided.
EmitAnyExprToTemp(const Expr * E)121 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
122 AggValueSlot AggSlot = AggValueSlot::ignored();
123
124 if (hasAggregateLLVMType(E->getType()) &&
125 !E->getType()->isAnyComplexType())
126 AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
127 return EmitAnyExpr(E, AggSlot);
128 }
129
130 /// EmitAnyExprToMem - Evaluate an expression into a given memory
131 /// location.
EmitAnyExprToMem(const Expr * E,llvm::Value * Location,Qualifiers Quals,bool IsInit)132 void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
133 llvm::Value *Location,
134 Qualifiers Quals,
135 bool IsInit) {
136 if (E->getType()->isAnyComplexType())
137 EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile());
138 else if (hasAggregateLLVMType(E->getType()))
139 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals, IsInit));
140 else {
141 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
142 LValue LV = MakeAddrLValue(Location, E->getType());
143 EmitStoreThroughLValue(RV, LV);
144 }
145 }
146
147 namespace {
148 /// \brief An adjustment to be made to the temporary created when emitting a
149 /// reference binding, which accesses a particular subobject of that temporary.
150 struct SubobjectAdjustment {
151 enum { DerivedToBaseAdjustment, FieldAdjustment } Kind;
152
153 union {
154 struct {
155 const CastExpr *BasePath;
156 const CXXRecordDecl *DerivedClass;
157 } DerivedToBase;
158
159 FieldDecl *Field;
160 };
161
SubobjectAdjustment__anon5713564c0111::SubobjectAdjustment162 SubobjectAdjustment(const CastExpr *BasePath,
163 const CXXRecordDecl *DerivedClass)
164 : Kind(DerivedToBaseAdjustment) {
165 DerivedToBase.BasePath = BasePath;
166 DerivedToBase.DerivedClass = DerivedClass;
167 }
168
SubobjectAdjustment__anon5713564c0111::SubobjectAdjustment169 SubobjectAdjustment(FieldDecl *Field)
170 : Kind(FieldAdjustment) {
171 this->Field = Field;
172 }
173 };
174 }
175
176 static llvm::Value *
CreateReferenceTemporary(CodeGenFunction & CGF,QualType Type,const NamedDecl * InitializedDecl)177 CreateReferenceTemporary(CodeGenFunction &CGF, QualType Type,
178 const NamedDecl *InitializedDecl) {
179 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
180 if (VD->hasGlobalStorage()) {
181 llvm::SmallString<256> Name;
182 llvm::raw_svector_ostream Out(Name);
183 CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out);
184 Out.flush();
185
186 llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type);
187
188 // Create the reference temporary.
189 llvm::GlobalValue *RefTemp =
190 new llvm::GlobalVariable(CGF.CGM.getModule(),
191 RefTempTy, /*isConstant=*/false,
192 llvm::GlobalValue::InternalLinkage,
193 llvm::Constant::getNullValue(RefTempTy),
194 Name.str());
195 return RefTemp;
196 }
197 }
198
199 return CGF.CreateMemTemp(Type, "ref.tmp");
200 }
201
202 static llvm::Value *
EmitExprForReferenceBinding(CodeGenFunction & CGF,const Expr * E,llvm::Value * & ReferenceTemporary,const CXXDestructorDecl * & ReferenceTemporaryDtor,QualType & ObjCARCReferenceLifetimeType,const NamedDecl * InitializedDecl)203 EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E,
204 llvm::Value *&ReferenceTemporary,
205 const CXXDestructorDecl *&ReferenceTemporaryDtor,
206 QualType &ObjCARCReferenceLifetimeType,
207 const NamedDecl *InitializedDecl) {
208 // Look through expressions for materialized temporaries (for now).
209 if (const MaterializeTemporaryExpr *M
210 = dyn_cast<MaterializeTemporaryExpr>(E)) {
211 // Objective-C++ ARC:
212 // If we are binding a reference to a temporary that has ownership, we
213 // need to perform retain/release operations on the temporary.
214 if (CGF.getContext().getLangOptions().ObjCAutoRefCount &&
215 E->getType()->isObjCLifetimeType() &&
216 (E->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
217 E->getType().getObjCLifetime() == Qualifiers::OCL_Weak ||
218 E->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing))
219 ObjCARCReferenceLifetimeType = E->getType();
220
221 E = M->GetTemporaryExpr();
222 }
223
224 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
225 E = DAE->getExpr();
226
227 if (const ExprWithCleanups *TE = dyn_cast<ExprWithCleanups>(E)) {
228 CodeGenFunction::RunCleanupsScope Scope(CGF);
229
230 return EmitExprForReferenceBinding(CGF, TE->getSubExpr(),
231 ReferenceTemporary,
232 ReferenceTemporaryDtor,
233 ObjCARCReferenceLifetimeType,
234 InitializedDecl);
235 }
236
237 if (const ObjCPropertyRefExpr *PRE =
238 dyn_cast<ObjCPropertyRefExpr>(E->IgnoreParenImpCasts()))
239 if (PRE->getGetterResultType()->isReferenceType())
240 E = PRE;
241
242 RValue RV;
243 if (E->isGLValue()) {
244 // Emit the expression as an lvalue.
245 LValue LV = CGF.EmitLValue(E);
246 if (LV.isPropertyRef()) {
247 RV = CGF.EmitLoadOfPropertyRefLValue(LV);
248 return RV.getScalarVal();
249 }
250
251 if (LV.isSimple())
252 return LV.getAddress();
253
254 // We have to load the lvalue.
255 RV = CGF.EmitLoadOfLValue(LV);
256 } else {
257 if (!ObjCARCReferenceLifetimeType.isNull()) {
258 ReferenceTemporary = CreateReferenceTemporary(CGF,
259 ObjCARCReferenceLifetimeType,
260 InitializedDecl);
261
262
263 LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary,
264 ObjCARCReferenceLifetimeType);
265
266 CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl),
267 RefTempDst, false);
268
269 bool ExtendsLifeOfTemporary = false;
270 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
271 if (Var->extendsLifetimeOfTemporary())
272 ExtendsLifeOfTemporary = true;
273 } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) {
274 ExtendsLifeOfTemporary = true;
275 }
276
277 if (!ExtendsLifeOfTemporary) {
278 // Since the lifetime of this temporary isn't going to be extended,
279 // we need to clean it up ourselves at the end of the full expression.
280 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
281 case Qualifiers::OCL_None:
282 case Qualifiers::OCL_ExplicitNone:
283 case Qualifiers::OCL_Autoreleasing:
284 break;
285
286 case Qualifiers::OCL_Strong: {
287 assert(!ObjCARCReferenceLifetimeType->isArrayType());
288 CleanupKind cleanupKind = CGF.getARCCleanupKind();
289 CGF.pushDestroy(cleanupKind,
290 ReferenceTemporary,
291 ObjCARCReferenceLifetimeType,
292 CodeGenFunction::destroyARCStrongImprecise,
293 cleanupKind & EHCleanup);
294 break;
295 }
296
297 case Qualifiers::OCL_Weak:
298 assert(!ObjCARCReferenceLifetimeType->isArrayType());
299 CGF.pushDestroy(NormalAndEHCleanup,
300 ReferenceTemporary,
301 ObjCARCReferenceLifetimeType,
302 CodeGenFunction::destroyARCWeak,
303 /*useEHCleanupForArray*/ true);
304 break;
305 }
306
307 ObjCARCReferenceLifetimeType = QualType();
308 }
309
310 return ReferenceTemporary;
311 }
312
313 llvm::SmallVector<SubobjectAdjustment, 2> Adjustments;
314 while (true) {
315 E = E->IgnoreParens();
316
317 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
318 if ((CE->getCastKind() == CK_DerivedToBase ||
319 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
320 E->getType()->isRecordType()) {
321 E = CE->getSubExpr();
322 CXXRecordDecl *Derived
323 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
324 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
325 continue;
326 }
327
328 if (CE->getCastKind() == CK_NoOp) {
329 E = CE->getSubExpr();
330 continue;
331 }
332 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
333 if (!ME->isArrow() && ME->getBase()->isRValue()) {
334 assert(ME->getBase()->getType()->isRecordType());
335 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
336 E = ME->getBase();
337 Adjustments.push_back(SubobjectAdjustment(Field));
338 continue;
339 }
340 }
341 }
342
343 if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E))
344 if (opaque->getType()->isRecordType())
345 return CGF.EmitOpaqueValueLValue(opaque).getAddress();
346
347 // Nothing changed.
348 break;
349 }
350
351 // Create a reference temporary if necessary.
352 AggValueSlot AggSlot = AggValueSlot::ignored();
353 if (CGF.hasAggregateLLVMType(E->getType()) &&
354 !E->getType()->isAnyComplexType()) {
355 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
356 InitializedDecl);
357 AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Qualifiers(),
358 InitializedDecl != 0);
359 }
360
361 if (InitializedDecl) {
362 // Get the destructor for the reference temporary.
363 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
364 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
365 if (!ClassDecl->hasTrivialDestructor())
366 ReferenceTemporaryDtor = ClassDecl->getDestructor();
367 }
368 }
369
370 RV = CGF.EmitAnyExpr(E, AggSlot);
371
372 // Check if need to perform derived-to-base casts and/or field accesses, to
373 // get from the temporary object we created (and, potentially, for which we
374 // extended the lifetime) to the subobject we're binding the reference to.
375 if (!Adjustments.empty()) {
376 llvm::Value *Object = RV.getAggregateAddr();
377 for (unsigned I = Adjustments.size(); I != 0; --I) {
378 SubobjectAdjustment &Adjustment = Adjustments[I-1];
379 switch (Adjustment.Kind) {
380 case SubobjectAdjustment::DerivedToBaseAdjustment:
381 Object =
382 CGF.GetAddressOfBaseClass(Object,
383 Adjustment.DerivedToBase.DerivedClass,
384 Adjustment.DerivedToBase.BasePath->path_begin(),
385 Adjustment.DerivedToBase.BasePath->path_end(),
386 /*NullCheckValue=*/false);
387 break;
388
389 case SubobjectAdjustment::FieldAdjustment: {
390 LValue LV =
391 CGF.EmitLValueForField(Object, Adjustment.Field, 0);
392 if (LV.isSimple()) {
393 Object = LV.getAddress();
394 break;
395 }
396
397 // For non-simple lvalues, we actually have to create a copy of
398 // the object we're binding to.
399 QualType T = Adjustment.Field->getType().getNonReferenceType()
400 .getUnqualifiedType();
401 Object = CreateReferenceTemporary(CGF, T, InitializedDecl);
402 LValue TempLV = CGF.MakeAddrLValue(Object,
403 Adjustment.Field->getType());
404 CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV);
405 break;
406 }
407
408 }
409 }
410
411 return Object;
412 }
413 }
414
415 if (RV.isAggregate())
416 return RV.getAggregateAddr();
417
418 // Create a temporary variable that we can bind the reference to.
419 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
420 InitializedDecl);
421
422
423 unsigned Alignment =
424 CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity();
425 if (RV.isScalar())
426 CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary,
427 /*Volatile=*/false, Alignment, E->getType());
428 else
429 CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary,
430 /*Volatile=*/false);
431 return ReferenceTemporary;
432 }
433
434 RValue
EmitReferenceBindingToExpr(const Expr * E,const NamedDecl * InitializedDecl)435 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E,
436 const NamedDecl *InitializedDecl) {
437 llvm::Value *ReferenceTemporary = 0;
438 const CXXDestructorDecl *ReferenceTemporaryDtor = 0;
439 QualType ObjCARCReferenceLifetimeType;
440 llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary,
441 ReferenceTemporaryDtor,
442 ObjCARCReferenceLifetimeType,
443 InitializedDecl);
444 if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull())
445 return RValue::get(Value);
446
447 // Make sure to call the destructor for the reference temporary.
448 const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl);
449 if (VD && VD->hasGlobalStorage()) {
450 if (ReferenceTemporaryDtor) {
451 llvm::Constant *DtorFn =
452 CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
453 EmitCXXGlobalDtorRegistration(DtorFn,
454 cast<llvm::Constant>(ReferenceTemporary));
455 } else {
456 assert(!ObjCARCReferenceLifetimeType.isNull());
457 // Note: We intentionally do not register a global "destructor" to
458 // release the object.
459 }
460
461 return RValue::get(Value);
462 }
463
464 if (ReferenceTemporaryDtor)
465 PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary);
466 else {
467 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
468 case Qualifiers::OCL_None:
469 assert(0 && "Not a reference temporary that needs to be deallocated");
470 case Qualifiers::OCL_ExplicitNone:
471 case Qualifiers::OCL_Autoreleasing:
472 // Nothing to do.
473 break;
474
475 case Qualifiers::OCL_Strong: {
476 bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>();
477 CleanupKind cleanupKind = getARCCleanupKind();
478 // This local is a GCC and MSVC compiler workaround.
479 Destroyer *destroyer = precise ? &destroyARCStrongPrecise :
480 &destroyARCStrongImprecise;
481 pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType,
482 *destroyer, cleanupKind & EHCleanup);
483 break;
484 }
485
486 case Qualifiers::OCL_Weak: {
487 // This local is a GCC and MSVC compiler workaround.
488 Destroyer *destroyer = &destroyARCWeak;
489 // __weak objects always get EH cleanups; otherwise, exceptions
490 // could cause really nasty crashes instead of mere leaks.
491 pushDestroy(NormalAndEHCleanup, ReferenceTemporary,
492 ObjCARCReferenceLifetimeType, *destroyer, true);
493 break;
494 }
495 }
496 }
497
498 return RValue::get(Value);
499 }
500
501
502 /// getAccessedFieldNo - Given an encoded value and a result number, return the
503 /// input field number being accessed.
getAccessedFieldNo(unsigned Idx,const llvm::Constant * Elts)504 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
505 const llvm::Constant *Elts) {
506 if (isa<llvm::ConstantAggregateZero>(Elts))
507 return 0;
508
509 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
510 }
511
EmitCheck(llvm::Value * Address,unsigned Size)512 void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
513 if (!CatchUndefined)
514 return;
515
516 // This needs to be to the standard address space.
517 Address = Builder.CreateBitCast(Address, Int8PtrTy);
518
519 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy);
520
521 // In time, people may want to control this and use a 1 here.
522 llvm::Value *Arg = Builder.getFalse();
523 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
524 llvm::BasicBlock *Cont = createBasicBlock();
525 llvm::BasicBlock *Check = createBasicBlock();
526 llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL);
527 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
528
529 EmitBlock(Check);
530 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
531 llvm::ConstantInt::get(IntPtrTy, Size)),
532 Cont, getTrapBB());
533 EmitBlock(Cont);
534 }
535
536
537 CodeGenFunction::ComplexPairTy CodeGenFunction::
EmitComplexPrePostIncDec(const UnaryOperator * E,LValue LV,bool isInc,bool isPre)538 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
539 bool isInc, bool isPre) {
540 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
541 LV.isVolatileQualified());
542
543 llvm::Value *NextVal;
544 if (isa<llvm::IntegerType>(InVal.first->getType())) {
545 uint64_t AmountVal = isInc ? 1 : -1;
546 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
547
548 // Add the inc/dec to the real part.
549 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
550 } else {
551 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
552 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
553 if (!isInc)
554 FVal.changeSign();
555 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
556
557 // Add the inc/dec to the real part.
558 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
559 }
560
561 ComplexPairTy IncVal(NextVal, InVal.second);
562
563 // Store the updated result through the lvalue.
564 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
565
566 // If this is a postinc, return the value read from memory, otherwise use the
567 // updated value.
568 return isPre ? IncVal : InVal;
569 }
570
571
572 //===----------------------------------------------------------------------===//
573 // LValue Expression Emission
574 //===----------------------------------------------------------------------===//
575
GetUndefRValue(QualType Ty)576 RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
577 if (Ty->isVoidType())
578 return RValue::get(0);
579
580 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
581 llvm::Type *EltTy = ConvertType(CTy->getElementType());
582 llvm::Value *U = llvm::UndefValue::get(EltTy);
583 return RValue::getComplex(std::make_pair(U, U));
584 }
585
586 // If this is a use of an undefined aggregate type, the aggregate must have an
587 // identifiable address. Just because the contents of the value are undefined
588 // doesn't mean that the address can't be taken and compared.
589 if (hasAggregateLLVMType(Ty)) {
590 llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
591 return RValue::getAggregate(DestPtr);
592 }
593
594 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
595 }
596
EmitUnsupportedRValue(const Expr * E,const char * Name)597 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
598 const char *Name) {
599 ErrorUnsupported(E, Name);
600 return GetUndefRValue(E->getType());
601 }
602
EmitUnsupportedLValue(const Expr * E,const char * Name)603 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
604 const char *Name) {
605 ErrorUnsupported(E, Name);
606 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
607 return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
608 }
609
EmitCheckedLValue(const Expr * E)610 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
611 LValue LV = EmitLValue(E);
612 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
613 EmitCheck(LV.getAddress(),
614 getContext().getTypeSizeInChars(E->getType()).getQuantity());
615 return LV;
616 }
617
618 /// EmitLValue - Emit code to compute a designator that specifies the location
619 /// of the expression.
620 ///
621 /// This can return one of two things: a simple address or a bitfield reference.
622 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be
623 /// an LLVM pointer type.
624 ///
625 /// If this returns a bitfield reference, nothing about the pointee type of the
626 /// LLVM value is known: For example, it may not be a pointer to an integer.
627 ///
628 /// If this returns a normal address, and if the lvalue's C type is fixed size,
629 /// this method guarantees that the returned pointer type will point to an LLVM
630 /// type of the same size of the lvalue's type. If the lvalue has a variable
631 /// length type, this is not possible.
632 ///
EmitLValue(const Expr * E)633 LValue CodeGenFunction::EmitLValue(const Expr *E) {
634 switch (E->getStmtClass()) {
635 default: return EmitUnsupportedLValue(E, "l-value expression");
636
637 case Expr::ObjCSelectorExprClass:
638 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
639 case Expr::ObjCIsaExprClass:
640 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
641 case Expr::BinaryOperatorClass:
642 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
643 case Expr::CompoundAssignOperatorClass:
644 if (!E->getType()->isAnyComplexType())
645 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
646 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
647 case Expr::CallExprClass:
648 case Expr::CXXMemberCallExprClass:
649 case Expr::CXXOperatorCallExprClass:
650 return EmitCallExprLValue(cast<CallExpr>(E));
651 case Expr::VAArgExprClass:
652 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
653 case Expr::DeclRefExprClass:
654 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
655 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
656 case Expr::GenericSelectionExprClass:
657 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
658 case Expr::PredefinedExprClass:
659 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
660 case Expr::StringLiteralClass:
661 return EmitStringLiteralLValue(cast<StringLiteral>(E));
662 case Expr::ObjCEncodeExprClass:
663 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
664
665 case Expr::BlockDeclRefExprClass:
666 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
667
668 case Expr::CXXTemporaryObjectExprClass:
669 case Expr::CXXConstructExprClass:
670 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
671 case Expr::CXXBindTemporaryExprClass:
672 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
673 case Expr::ExprWithCleanupsClass:
674 return EmitExprWithCleanupsLValue(cast<ExprWithCleanups>(E));
675 case Expr::CXXScalarValueInitExprClass:
676 return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E));
677 case Expr::CXXDefaultArgExprClass:
678 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
679 case Expr::CXXTypeidExprClass:
680 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
681
682 case Expr::ObjCMessageExprClass:
683 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
684 case Expr::ObjCIvarRefExprClass:
685 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
686 case Expr::ObjCPropertyRefExprClass:
687 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
688 case Expr::StmtExprClass:
689 return EmitStmtExprLValue(cast<StmtExpr>(E));
690 case Expr::UnaryOperatorClass:
691 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
692 case Expr::ArraySubscriptExprClass:
693 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
694 case Expr::ExtVectorElementExprClass:
695 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
696 case Expr::MemberExprClass:
697 return EmitMemberExpr(cast<MemberExpr>(E));
698 case Expr::CompoundLiteralExprClass:
699 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
700 case Expr::ConditionalOperatorClass:
701 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
702 case Expr::BinaryConditionalOperatorClass:
703 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
704 case Expr::ChooseExprClass:
705 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
706 case Expr::OpaqueValueExprClass:
707 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
708 case Expr::SubstNonTypeTemplateParmExprClass:
709 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
710 case Expr::ImplicitCastExprClass:
711 case Expr::CStyleCastExprClass:
712 case Expr::CXXFunctionalCastExprClass:
713 case Expr::CXXStaticCastExprClass:
714 case Expr::CXXDynamicCastExprClass:
715 case Expr::CXXReinterpretCastExprClass:
716 case Expr::CXXConstCastExprClass:
717 case Expr::ObjCBridgedCastExprClass:
718 return EmitCastLValue(cast<CastExpr>(E));
719
720 case Expr::MaterializeTemporaryExprClass:
721 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
722 }
723 }
724
EmitLoadOfScalar(LValue lvalue)725 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) {
726 return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
727 lvalue.getAlignment(), lvalue.getType(),
728 lvalue.getTBAAInfo());
729 }
730
EmitLoadOfScalar(llvm::Value * Addr,bool Volatile,unsigned Alignment,QualType Ty,llvm::MDNode * TBAAInfo)731 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
732 unsigned Alignment, QualType Ty,
733 llvm::MDNode *TBAAInfo) {
734 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
735 if (Volatile)
736 Load->setVolatile(true);
737 if (Alignment)
738 Load->setAlignment(Alignment);
739 if (TBAAInfo)
740 CGM.DecorateInstruction(Load, TBAAInfo);
741
742 return EmitFromMemory(Load, Ty);
743 }
744
isBooleanUnderlyingType(QualType Ty)745 static bool isBooleanUnderlyingType(QualType Ty) {
746 if (const EnumType *ET = dyn_cast<EnumType>(Ty))
747 return ET->getDecl()->getIntegerType()->isBooleanType();
748 return false;
749 }
750
EmitToMemory(llvm::Value * Value,QualType Ty)751 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
752 // Bool has a different representation in memory than in registers.
753 if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
754 // This should really always be an i1, but sometimes it's already
755 // an i8, and it's awkward to track those cases down.
756 if (Value->getType()->isIntegerTy(1))
757 return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool");
758 assert(Value->getType()->isIntegerTy(8) && "value rep of bool not i1/i8");
759 }
760
761 return Value;
762 }
763
EmitFromMemory(llvm::Value * Value,QualType Ty)764 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
765 // Bool has a different representation in memory than in registers.
766 if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
767 assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8");
768 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
769 }
770
771 return Value;
772 }
773
EmitStoreOfScalar(llvm::Value * Value,llvm::Value * Addr,bool Volatile,unsigned Alignment,QualType Ty,llvm::MDNode * TBAAInfo)774 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
775 bool Volatile, unsigned Alignment,
776 QualType Ty,
777 llvm::MDNode *TBAAInfo) {
778 Value = EmitToMemory(Value, Ty);
779
780 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
781 if (Alignment)
782 Store->setAlignment(Alignment);
783 if (TBAAInfo)
784 CGM.DecorateInstruction(Store, TBAAInfo);
785 }
786
EmitStoreOfScalar(llvm::Value * value,LValue lvalue)787 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue) {
788 EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
789 lvalue.getAlignment(), lvalue.getType(),
790 lvalue.getTBAAInfo());
791 }
792
793 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
794 /// method emits the address of the lvalue, then loads the result as an rvalue,
795 /// returning the rvalue.
EmitLoadOfLValue(LValue LV)796 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) {
797 if (LV.isObjCWeak()) {
798 // load of a __weak object.
799 llvm::Value *AddrWeakObj = LV.getAddress();
800 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
801 AddrWeakObj));
802 }
803 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak)
804 return RValue::get(EmitARCLoadWeak(LV.getAddress()));
805
806 if (LV.isSimple()) {
807 assert(!LV.getType()->isFunctionType());
808
809 // Everything needs a load.
810 return RValue::get(EmitLoadOfScalar(LV));
811 }
812
813 if (LV.isVectorElt()) {
814 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
815 LV.isVolatileQualified(), "tmp");
816 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
817 "vecext"));
818 }
819
820 // If this is a reference to a subset of the elements of a vector, either
821 // shuffle the input or extract/insert them as appropriate.
822 if (LV.isExtVectorElt())
823 return EmitLoadOfExtVectorElementLValue(LV);
824
825 if (LV.isBitField())
826 return EmitLoadOfBitfieldLValue(LV);
827
828 assert(LV.isPropertyRef() && "Unknown LValue type!");
829 return EmitLoadOfPropertyRefLValue(LV);
830 }
831
EmitLoadOfBitfieldLValue(LValue LV)832 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
833 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
834
835 // Get the output type.
836 llvm::Type *ResLTy = ConvertType(LV.getType());
837 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
838
839 // Compute the result as an OR of all of the individual component accesses.
840 llvm::Value *Res = 0;
841 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
842 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
843
844 // Get the field pointer.
845 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
846
847 // Only offset by the field index if used, so that incoming values are not
848 // required to be structures.
849 if (AI.FieldIndex)
850 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
851
852 // Offset by the byte offset, if used.
853 if (!AI.FieldByteOffset.isZero()) {
854 Ptr = EmitCastToVoidPtr(Ptr);
855 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
856 "bf.field.offs");
857 }
858
859 // Cast to the access type.
860 llvm::Type *PTy = llvm::Type::getIntNPtrTy(getLLVMContext(),
861 AI.AccessWidth,
862 CGM.getContext().getTargetAddressSpace(LV.getType()));
863 Ptr = Builder.CreateBitCast(Ptr, PTy);
864
865 // Perform the load.
866 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
867 if (!AI.AccessAlignment.isZero())
868 Load->setAlignment(AI.AccessAlignment.getQuantity());
869
870 // Shift out unused low bits and mask out unused high bits.
871 llvm::Value *Val = Load;
872 if (AI.FieldBitStart)
873 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
874 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
875 AI.TargetBitWidth),
876 "bf.clear");
877
878 // Extend or truncate to the target size.
879 if (AI.AccessWidth < ResSizeInBits)
880 Val = Builder.CreateZExt(Val, ResLTy);
881 else if (AI.AccessWidth > ResSizeInBits)
882 Val = Builder.CreateTrunc(Val, ResLTy);
883
884 // Shift into place, and OR into the result.
885 if (AI.TargetBitOffset)
886 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
887 Res = Res ? Builder.CreateOr(Res, Val) : Val;
888 }
889
890 // If the bit-field is signed, perform the sign-extension.
891 //
892 // FIXME: This can easily be folded into the load of the high bits, which
893 // could also eliminate the mask of high bits in some situations.
894 if (Info.isSigned()) {
895 unsigned ExtraBits = ResSizeInBits - Info.getSize();
896 if (ExtraBits)
897 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
898 ExtraBits, "bf.val.sext");
899 }
900
901 return RValue::get(Res);
902 }
903
904 // If this is a reference to a subset of the elements of a vector, create an
905 // appropriate shufflevector.
EmitLoadOfExtVectorElementLValue(LValue LV)906 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
907 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
908 LV.isVolatileQualified(), "tmp");
909
910 const llvm::Constant *Elts = LV.getExtVectorElts();
911
912 // If the result of the expression is a non-vector type, we must be extracting
913 // a single element. Just codegen as an extractelement.
914 const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
915 if (!ExprVT) {
916 unsigned InIdx = getAccessedFieldNo(0, Elts);
917 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
918 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
919 }
920
921 // Always use shuffle vector to try to retain the original program structure
922 unsigned NumResultElts = ExprVT->getNumElements();
923
924 llvm::SmallVector<llvm::Constant*, 4> Mask;
925 for (unsigned i = 0; i != NumResultElts; ++i) {
926 unsigned InIdx = getAccessedFieldNo(i, Elts);
927 Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx));
928 }
929
930 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
931 Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
932 MaskV, "tmp");
933 return RValue::get(Vec);
934 }
935
936
937
938 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
939 /// lvalue, where both are guaranteed to the have the same type, and that type
940 /// is 'Ty'.
EmitStoreThroughLValue(RValue Src,LValue Dst)941 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst) {
942 if (!Dst.isSimple()) {
943 if (Dst.isVectorElt()) {
944 // Read/modify/write the vector, inserting the new element.
945 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
946 Dst.isVolatileQualified(), "tmp");
947 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
948 Dst.getVectorIdx(), "vecins");
949 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
950 return;
951 }
952
953 // If this is an update of extended vector elements, insert them as
954 // appropriate.
955 if (Dst.isExtVectorElt())
956 return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
957
958 if (Dst.isBitField())
959 return EmitStoreThroughBitfieldLValue(Src, Dst);
960
961 assert(Dst.isPropertyRef() && "Unknown LValue type");
962 return EmitStoreThroughPropertyRefLValue(Src, Dst);
963 }
964
965 // There's special magic for assigning into an ARC-qualified l-value.
966 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
967 switch (Lifetime) {
968 case Qualifiers::OCL_None:
969 llvm_unreachable("present but none");
970
971 case Qualifiers::OCL_ExplicitNone:
972 // nothing special
973 break;
974
975 case Qualifiers::OCL_Strong:
976 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
977 return;
978
979 case Qualifiers::OCL_Weak:
980 EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
981 return;
982
983 case Qualifiers::OCL_Autoreleasing:
984 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
985 Src.getScalarVal()));
986 // fall into the normal path
987 break;
988 }
989 }
990
991 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
992 // load of a __weak object.
993 llvm::Value *LvalueDst = Dst.getAddress();
994 llvm::Value *src = Src.getScalarVal();
995 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
996 return;
997 }
998
999 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
1000 // load of a __strong object.
1001 llvm::Value *LvalueDst = Dst.getAddress();
1002 llvm::Value *src = Src.getScalarVal();
1003 if (Dst.isObjCIvar()) {
1004 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1005 llvm::Type *ResultType = ConvertType(getContext().LongTy);
1006 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1007 llvm::Value *dst = RHS;
1008 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1009 llvm::Value *LHS =
1010 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1011 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1012 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1013 BytesBetween);
1014 } else if (Dst.isGlobalObjCRef()) {
1015 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1016 Dst.isThreadLocalRef());
1017 }
1018 else
1019 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1020 return;
1021 }
1022
1023 assert(Src.isScalar() && "Can't emit an agg store with this method");
1024 EmitStoreOfScalar(Src.getScalarVal(), Dst);
1025 }
1026
EmitStoreThroughBitfieldLValue(RValue Src,LValue Dst,llvm::Value ** Result)1027 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1028 llvm::Value **Result) {
1029 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1030
1031 // Get the output type.
1032 llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1033 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
1034
1035 // Get the source value, truncated to the width of the bit-field.
1036 llvm::Value *SrcVal = Src.getScalarVal();
1037
1038 if (Dst.getType()->isBooleanType())
1039 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
1040
1041 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
1042 Info.getSize()),
1043 "bf.value");
1044
1045 // Return the new value of the bit-field, if requested.
1046 if (Result) {
1047 // Cast back to the proper type for result.
1048 llvm::Type *SrcTy = Src.getScalarVal()->getType();
1049 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
1050 "bf.reload.val");
1051
1052 // Sign extend if necessary.
1053 if (Info.isSigned()) {
1054 unsigned ExtraBits = ResSizeInBits - Info.getSize();
1055 if (ExtraBits)
1056 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
1057 ExtraBits, "bf.reload.sext");
1058 }
1059
1060 *Result = ReloadVal;
1061 }
1062
1063 // Iterate over the components, writing each piece to memory.
1064 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1065 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1066
1067 // Get the field pointer.
1068 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
1069 unsigned addressSpace =
1070 cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1071
1072 // Only offset by the field index if used, so that incoming values are not
1073 // required to be structures.
1074 if (AI.FieldIndex)
1075 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
1076
1077 // Offset by the byte offset, if used.
1078 if (!AI.FieldByteOffset.isZero()) {
1079 Ptr = EmitCastToVoidPtr(Ptr);
1080 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
1081 "bf.field.offs");
1082 }
1083
1084 // Cast to the access type.
1085 llvm::Type *AccessLTy =
1086 llvm::Type::getIntNTy(getLLVMContext(), AI.AccessWidth);
1087
1088 llvm::Type *PTy = AccessLTy->getPointerTo(addressSpace);
1089 Ptr = Builder.CreateBitCast(Ptr, PTy);
1090
1091 // Extract the piece of the bit-field value to write in this access, limited
1092 // to the values that are part of this access.
1093 llvm::Value *Val = SrcVal;
1094 if (AI.TargetBitOffset)
1095 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
1096 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
1097 AI.TargetBitWidth));
1098
1099 // Extend or truncate to the access size.
1100 if (ResSizeInBits < AI.AccessWidth)
1101 Val = Builder.CreateZExt(Val, AccessLTy);
1102 else if (ResSizeInBits > AI.AccessWidth)
1103 Val = Builder.CreateTrunc(Val, AccessLTy);
1104
1105 // Shift into the position in memory.
1106 if (AI.FieldBitStart)
1107 Val = Builder.CreateShl(Val, AI.FieldBitStart);
1108
1109 // If necessary, load and OR in bits that are outside of the bit-field.
1110 if (AI.TargetBitWidth != AI.AccessWidth) {
1111 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
1112 if (!AI.AccessAlignment.isZero())
1113 Load->setAlignment(AI.AccessAlignment.getQuantity());
1114
1115 // Compute the mask for zeroing the bits that are part of the bit-field.
1116 llvm::APInt InvMask =
1117 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
1118 AI.FieldBitStart + AI.TargetBitWidth);
1119
1120 // Apply the mask and OR in to the value to write.
1121 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
1122 }
1123
1124 // Write the value.
1125 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
1126 Dst.isVolatileQualified());
1127 if (!AI.AccessAlignment.isZero())
1128 Store->setAlignment(AI.AccessAlignment.getQuantity());
1129 }
1130 }
1131
EmitStoreThroughExtVectorComponentLValue(RValue Src,LValue Dst)1132 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1133 LValue Dst) {
1134 // This access turns into a read/modify/write of the vector. Load the input
1135 // value now.
1136 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
1137 Dst.isVolatileQualified(), "tmp");
1138 const llvm::Constant *Elts = Dst.getExtVectorElts();
1139
1140 llvm::Value *SrcVal = Src.getScalarVal();
1141
1142 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1143 unsigned NumSrcElts = VTy->getNumElements();
1144 unsigned NumDstElts =
1145 cast<llvm::VectorType>(Vec->getType())->getNumElements();
1146 if (NumDstElts == NumSrcElts) {
1147 // Use shuffle vector is the src and destination are the same number of
1148 // elements and restore the vector mask since it is on the side it will be
1149 // stored.
1150 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1151 for (unsigned i = 0; i != NumSrcElts; ++i) {
1152 unsigned InIdx = getAccessedFieldNo(i, Elts);
1153 Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i);
1154 }
1155
1156 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1157 Vec = Builder.CreateShuffleVector(SrcVal,
1158 llvm::UndefValue::get(Vec->getType()),
1159 MaskV, "tmp");
1160 } else if (NumDstElts > NumSrcElts) {
1161 // Extended the source vector to the same length and then shuffle it
1162 // into the destination.
1163 // FIXME: since we're shuffling with undef, can we just use the indices
1164 // into that? This could be simpler.
1165 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
1166 unsigned i;
1167 for (i = 0; i != NumSrcElts; ++i)
1168 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1169 for (; i != NumDstElts; ++i)
1170 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
1171 llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1172 llvm::Value *ExtSrcVal =
1173 Builder.CreateShuffleVector(SrcVal,
1174 llvm::UndefValue::get(SrcVal->getType()),
1175 ExtMaskV, "tmp");
1176 // build identity
1177 llvm::SmallVector<llvm::Constant*, 4> Mask;
1178 for (unsigned i = 0; i != NumDstElts; ++i)
1179 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1180
1181 // modify when what gets shuffled in
1182 for (unsigned i = 0; i != NumSrcElts; ++i) {
1183 unsigned Idx = getAccessedFieldNo(i, Elts);
1184 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
1185 }
1186 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1187 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
1188 } else {
1189 // We should never shorten the vector
1190 assert(0 && "unexpected shorten vector length");
1191 }
1192 } else {
1193 // If the Src is a scalar (not a vector) it must be updating one element.
1194 unsigned InIdx = getAccessedFieldNo(0, Elts);
1195 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1196 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
1197 }
1198
1199 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
1200 }
1201
1202 // setObjCGCLValueClass - sets class of he lvalue for the purpose of
1203 // generating write-barries API. It is currently a global, ivar,
1204 // or neither.
setObjCGCLValueClass(const ASTContext & Ctx,const Expr * E,LValue & LV)1205 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1206 LValue &LV) {
1207 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
1208 return;
1209
1210 if (isa<ObjCIvarRefExpr>(E)) {
1211 LV.setObjCIvar(true);
1212 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1213 LV.setBaseIvarExp(Exp->getBase());
1214 LV.setObjCArray(E->getType()->isArrayType());
1215 return;
1216 }
1217
1218 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1219 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1220 if (VD->hasGlobalStorage()) {
1221 LV.setGlobalObjCRef(true);
1222 LV.setThreadLocalRef(VD->isThreadSpecified());
1223 }
1224 }
1225 LV.setObjCArray(E->getType()->isArrayType());
1226 return;
1227 }
1228
1229 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1230 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1231 return;
1232 }
1233
1234 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1235 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1236 if (LV.isObjCIvar()) {
1237 // If cast is to a structure pointer, follow gcc's behavior and make it
1238 // a non-ivar write-barrier.
1239 QualType ExpTy = E->getType();
1240 if (ExpTy->isPointerType())
1241 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1242 if (ExpTy->isRecordType())
1243 LV.setObjCIvar(false);
1244 }
1245 return;
1246 }
1247
1248 if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1249 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1250 return;
1251 }
1252
1253 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1254 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1255 return;
1256 }
1257
1258 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1259 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1260 return;
1261 }
1262
1263 if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1264 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
1265 return;
1266 }
1267
1268 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1269 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1270 if (LV.isObjCIvar() && !LV.isObjCArray())
1271 // Using array syntax to assigning to what an ivar points to is not
1272 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1273 LV.setObjCIvar(false);
1274 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1275 // Using array syntax to assigning to what global points to is not
1276 // same as assigning to the global itself. {id *G;} G[i] = 0;
1277 LV.setGlobalObjCRef(false);
1278 return;
1279 }
1280
1281 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1282 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1283 // We don't know if member is an 'ivar', but this flag is looked at
1284 // only in the context of LV.isObjCIvar().
1285 LV.setObjCArray(E->getType()->isArrayType());
1286 return;
1287 }
1288 }
1289
1290 static llvm::Value *
EmitBitCastOfLValueToProperType(CodeGenFunction & CGF,llvm::Value * V,llvm::Type * IRType,llvm::StringRef Name=llvm::StringRef ())1291 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1292 llvm::Value *V, llvm::Type *IRType,
1293 llvm::StringRef Name = llvm::StringRef()) {
1294 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1295 return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1296 }
1297
EmitGlobalVarDeclLValue(CodeGenFunction & CGF,const Expr * E,const VarDecl * VD)1298 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1299 const Expr *E, const VarDecl *VD) {
1300 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
1301 "Var decl must have external storage or be a file var decl!");
1302
1303 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1304 if (VD->getType()->isReferenceType())
1305 V = CGF.Builder.CreateLoad(V, "tmp");
1306
1307 V = EmitBitCastOfLValueToProperType(CGF, V,
1308 CGF.getTypes().ConvertTypeForMem(E->getType()));
1309
1310 unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
1311 LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1312 setObjCGCLValueClass(CGF.getContext(), E, LV);
1313 return LV;
1314 }
1315
EmitFunctionDeclLValue(CodeGenFunction & CGF,const Expr * E,const FunctionDecl * FD)1316 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1317 const Expr *E, const FunctionDecl *FD) {
1318 llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1319 if (!FD->hasPrototype()) {
1320 if (const FunctionProtoType *Proto =
1321 FD->getType()->getAs<FunctionProtoType>()) {
1322 // Ugly case: for a K&R-style definition, the type of the definition
1323 // isn't the same as the type of a use. Correct for this with a
1324 // bitcast.
1325 QualType NoProtoType =
1326 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1327 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1328 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1329 }
1330 }
1331 unsigned Alignment = CGF.getContext().getDeclAlign(FD).getQuantity();
1332 return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1333 }
1334
EmitDeclRefLValue(const DeclRefExpr * E)1335 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1336 const NamedDecl *ND = E->getDecl();
1337 unsigned Alignment = getContext().getDeclAlign(ND).getQuantity();
1338
1339 if (ND->hasAttr<WeakRefAttr>()) {
1340 const ValueDecl *VD = cast<ValueDecl>(ND);
1341 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1342 return MakeAddrLValue(Aliasee, E->getType(), Alignment);
1343 }
1344
1345 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1346
1347 // Check if this is a global variable.
1348 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1349 return EmitGlobalVarDeclLValue(*this, E, VD);
1350
1351 bool NonGCable = VD->hasLocalStorage() &&
1352 !VD->getType()->isReferenceType() &&
1353 !VD->hasAttr<BlocksAttr>();
1354
1355 llvm::Value *V = LocalDeclMap[VD];
1356 if (!V && VD->isStaticLocal())
1357 V = CGM.getStaticLocalDeclAddress(VD);
1358 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1359
1360 if (VD->hasAttr<BlocksAttr>())
1361 V = BuildBlockByrefAddress(V, VD);
1362
1363 if (VD->getType()->isReferenceType())
1364 V = Builder.CreateLoad(V, "tmp");
1365
1366 V = EmitBitCastOfLValueToProperType(*this, V,
1367 getTypes().ConvertTypeForMem(E->getType()));
1368
1369 LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
1370 if (NonGCable) {
1371 LV.getQuals().removeObjCGCAttr();
1372 LV.setNonGC(true);
1373 }
1374 setObjCGCLValueClass(getContext(), E, LV);
1375 return LV;
1376 }
1377
1378 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND))
1379 return EmitFunctionDeclLValue(*this, E, fn);
1380
1381 assert(false && "Unhandled DeclRefExpr");
1382
1383 // an invalid LValue, but the assert will
1384 // ensure that this point is never reached.
1385 return LValue();
1386 }
1387
EmitBlockDeclRefLValue(const BlockDeclRefExpr * E)1388 LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
1389 unsigned Alignment =
1390 getContext().getDeclAlign(E->getDecl()).getQuantity();
1391 return MakeAddrLValue(GetAddrOfBlockDecl(E), E->getType(), Alignment);
1392 }
1393
EmitUnaryOpLValue(const UnaryOperator * E)1394 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1395 // __extension__ doesn't affect lvalue-ness.
1396 if (E->getOpcode() == UO_Extension)
1397 return EmitLValue(E->getSubExpr());
1398
1399 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1400 switch (E->getOpcode()) {
1401 default: assert(0 && "Unknown unary operator lvalue!");
1402 case UO_Deref: {
1403 QualType T = E->getSubExpr()->getType()->getPointeeType();
1404 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1405
1406 LValue LV = MakeAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1407 LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1408
1409 // We should not generate __weak write barrier on indirect reference
1410 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1411 // But, we continue to generate __strong write barrier on indirect write
1412 // into a pointer to object.
1413 if (getContext().getLangOptions().ObjC1 &&
1414 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1415 LV.isObjCWeak())
1416 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1417 return LV;
1418 }
1419 case UO_Real:
1420 case UO_Imag: {
1421 LValue LV = EmitLValue(E->getSubExpr());
1422 assert(LV.isSimple() && "real/imag on non-ordinary l-value");
1423 llvm::Value *Addr = LV.getAddress();
1424
1425 // real and imag are valid on scalars. This is a faster way of
1426 // testing that.
1427 if (!cast<llvm::PointerType>(Addr->getType())
1428 ->getElementType()->isStructTy()) {
1429 assert(E->getSubExpr()->getType()->isArithmeticType());
1430 return LV;
1431 }
1432
1433 assert(E->getSubExpr()->getType()->isAnyComplexType());
1434
1435 unsigned Idx = E->getOpcode() == UO_Imag;
1436 return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1437 Idx, "idx"),
1438 ExprTy);
1439 }
1440 case UO_PreInc:
1441 case UO_PreDec: {
1442 LValue LV = EmitLValue(E->getSubExpr());
1443 bool isInc = E->getOpcode() == UO_PreInc;
1444
1445 if (E->getType()->isAnyComplexType())
1446 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1447 else
1448 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1449 return LV;
1450 }
1451 }
1452 }
1453
EmitStringLiteralLValue(const StringLiteral * E)1454 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1455 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1456 E->getType());
1457 }
1458
EmitObjCEncodeExprLValue(const ObjCEncodeExpr * E)1459 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1460 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1461 E->getType());
1462 }
1463
1464
EmitPredefinedLValue(const PredefinedExpr * E)1465 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1466 switch (E->getIdentType()) {
1467 default:
1468 return EmitUnsupportedLValue(E, "predefined expression");
1469
1470 case PredefinedExpr::Func:
1471 case PredefinedExpr::Function:
1472 case PredefinedExpr::PrettyFunction: {
1473 unsigned Type = E->getIdentType();
1474 std::string GlobalVarName;
1475
1476 switch (Type) {
1477 default: assert(0 && "Invalid type");
1478 case PredefinedExpr::Func:
1479 GlobalVarName = "__func__.";
1480 break;
1481 case PredefinedExpr::Function:
1482 GlobalVarName = "__FUNCTION__.";
1483 break;
1484 case PredefinedExpr::PrettyFunction:
1485 GlobalVarName = "__PRETTY_FUNCTION__.";
1486 break;
1487 }
1488
1489 llvm::StringRef FnName = CurFn->getName();
1490 if (FnName.startswith("\01"))
1491 FnName = FnName.substr(1);
1492 GlobalVarName += FnName;
1493
1494 const Decl *CurDecl = CurCodeDecl;
1495 if (CurDecl == 0)
1496 CurDecl = getContext().getTranslationUnitDecl();
1497
1498 std::string FunctionName =
1499 (isa<BlockDecl>(CurDecl)
1500 ? FnName.str()
1501 : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurDecl));
1502
1503 llvm::Constant *C =
1504 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
1505 return MakeAddrLValue(C, E->getType());
1506 }
1507 }
1508 }
1509
getTrapBB()1510 llvm::BasicBlock *CodeGenFunction::getTrapBB() {
1511 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1512
1513 // If we are not optimzing, don't collapse all calls to trap in the function
1514 // to the same call, that way, in the debugger they can see which operation
1515 // did in fact fail. If we are optimizing, we collapse all calls to trap down
1516 // to just one per function to save on codesize.
1517 if (GCO.OptimizationLevel && TrapBB)
1518 return TrapBB;
1519
1520 llvm::BasicBlock *Cont = 0;
1521 if (HaveInsertPoint()) {
1522 Cont = createBasicBlock("cont");
1523 EmitBranch(Cont);
1524 }
1525 TrapBB = createBasicBlock("trap");
1526 EmitBlock(TrapBB);
1527
1528 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
1529 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1530 TrapCall->setDoesNotReturn();
1531 TrapCall->setDoesNotThrow();
1532 Builder.CreateUnreachable();
1533
1534 if (Cont)
1535 EmitBlock(Cont);
1536 return TrapBB;
1537 }
1538
1539 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
1540 /// array to pointer, return the array subexpression.
isSimpleArrayDecayOperand(const Expr * E)1541 static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
1542 // If this isn't just an array->pointer decay, bail out.
1543 const CastExpr *CE = dyn_cast<CastExpr>(E);
1544 if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
1545 return 0;
1546
1547 // If this is a decay from variable width array, bail out.
1548 const Expr *SubExpr = CE->getSubExpr();
1549 if (SubExpr->getType()->isVariableArrayType())
1550 return 0;
1551
1552 return SubExpr;
1553 }
1554
EmitArraySubscriptExpr(const ArraySubscriptExpr * E)1555 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1556 // The index must always be an integer, which is not an aggregate. Emit it.
1557 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
1558 QualType IdxTy = E->getIdx()->getType();
1559 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
1560
1561 // If the base is a vector type, then we are forming a vector element lvalue
1562 // with this subscript.
1563 if (E->getBase()->getType()->isVectorType()) {
1564 // Emit the vector as an lvalue to get its address.
1565 LValue LHS = EmitLValue(E->getBase());
1566 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
1567 Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx");
1568 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
1569 E->getBase()->getType());
1570 }
1571
1572 // Extend or truncate the index type to 32 or 64-bits.
1573 if (Idx->getType() != IntPtrTy)
1574 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
1575
1576 // FIXME: As llvm implements the object size checking, this can come out.
1577 if (CatchUndefined) {
1578 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){
1579 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1580 if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
1581 if (const ConstantArrayType *CAT
1582 = getContext().getAsConstantArrayType(DRE->getType())) {
1583 llvm::APInt Size = CAT->getSize();
1584 llvm::BasicBlock *Cont = createBasicBlock("cont");
1585 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
1586 llvm::ConstantInt::get(Idx->getType(), Size)),
1587 Cont, getTrapBB());
1588 EmitBlock(Cont);
1589 }
1590 }
1591 }
1592 }
1593 }
1594
1595 // We know that the pointer points to a type of the correct size, unless the
1596 // size is a VLA or Objective-C interface.
1597 llvm::Value *Address = 0;
1598 unsigned ArrayAlignment = 0;
1599 if (const VariableArrayType *vla =
1600 getContext().getAsVariableArrayType(E->getType())) {
1601 // The base must be a pointer, which is not an aggregate. Emit
1602 // it. It needs to be emitted first in case it's what captures
1603 // the VLA bounds.
1604 Address = EmitScalarExpr(E->getBase());
1605
1606 // The element count here is the total number of non-VLA elements.
1607 llvm::Value *numElements = getVLASize(vla).first;
1608
1609 // Effectively, the multiply by the VLA size is part of the GEP.
1610 // GEP indexes are signed, and scaling an index isn't permitted to
1611 // signed-overflow, so we use the same semantics for our explicit
1612 // multiply. We suppress this if overflow is not undefined behavior.
1613 if (getLangOptions().isSignedOverflowDefined()) {
1614 Idx = Builder.CreateMul(Idx, numElements);
1615 Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1616 } else {
1617 Idx = Builder.CreateNSWMul(Idx, numElements);
1618 Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
1619 }
1620 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
1621 // Indexing over an interface, as in "NSString *P; P[4];"
1622 llvm::Value *InterfaceSize =
1623 llvm::ConstantInt::get(Idx->getType(),
1624 getContext().getTypeSizeInChars(OIT).getQuantity());
1625
1626 Idx = Builder.CreateMul(Idx, InterfaceSize);
1627
1628 // The base must be a pointer, which is not an aggregate. Emit it.
1629 llvm::Value *Base = EmitScalarExpr(E->getBase());
1630 Address = EmitCastToVoidPtr(Base);
1631 Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1632 Address = Builder.CreateBitCast(Address, Base->getType());
1633 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
1634 // If this is A[i] where A is an array, the frontend will have decayed the
1635 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
1636 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
1637 // "gep x, i" here. Emit one "gep A, 0, i".
1638 assert(Array->getType()->isArrayType() &&
1639 "Array to pointer decay must have array source type!");
1640 LValue ArrayLV = EmitLValue(Array);
1641 llvm::Value *ArrayPtr = ArrayLV.getAddress();
1642 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
1643 llvm::Value *Args[] = { Zero, Idx };
1644
1645 // Propagate the alignment from the array itself to the result.
1646 ArrayAlignment = ArrayLV.getAlignment();
1647
1648 if (getContext().getLangOptions().isSignedOverflowDefined())
1649 Address = Builder.CreateGEP(ArrayPtr, Args, Args+2, "arrayidx");
1650 else
1651 Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, Args+2, "arrayidx");
1652 } else {
1653 // The base must be a pointer, which is not an aggregate. Emit it.
1654 llvm::Value *Base = EmitScalarExpr(E->getBase());
1655 if (getContext().getLangOptions().isSignedOverflowDefined())
1656 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
1657 else
1658 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1659 }
1660
1661 QualType T = E->getBase()->getType()->getPointeeType();
1662 assert(!T.isNull() &&
1663 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
1664
1665 // Limit the alignment to that of the result type.
1666 if (ArrayAlignment) {
1667 unsigned Align = getContext().getTypeAlignInChars(T).getQuantity();
1668 ArrayAlignment = std::min(Align, ArrayAlignment);
1669 }
1670
1671 LValue LV = MakeAddrLValue(Address, T, ArrayAlignment);
1672 LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
1673
1674 if (getContext().getLangOptions().ObjC1 &&
1675 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
1676 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1677 setObjCGCLValueClass(getContext(), E, LV);
1678 }
1679 return LV;
1680 }
1681
1682 static
GenerateConstantVector(llvm::LLVMContext & VMContext,llvm::SmallVector<unsigned,4> & Elts)1683 llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1684 llvm::SmallVector<unsigned, 4> &Elts) {
1685 llvm::SmallVector<llvm::Constant*, 4> CElts;
1686
1687 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1688 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
1689 CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i]));
1690
1691 return llvm::ConstantVector::get(CElts);
1692 }
1693
1694 LValue CodeGenFunction::
EmitExtVectorElementExpr(const ExtVectorElementExpr * E)1695 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
1696 // Emit the base vector as an l-value.
1697 LValue Base;
1698
1699 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1700 if (E->isArrow()) {
1701 // If it is a pointer to a vector, emit the address and form an lvalue with
1702 // it.
1703 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
1704 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
1705 Base = MakeAddrLValue(Ptr, PT->getPointeeType());
1706 Base.getQuals().removeObjCGCAttr();
1707 } else if (E->getBase()->isGLValue()) {
1708 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1709 // emit the base as an lvalue.
1710 assert(E->getBase()->getType()->isVectorType());
1711 Base = EmitLValue(E->getBase());
1712 } else {
1713 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1714 assert(E->getBase()->getType()->isVectorType() &&
1715 "Result must be a vector");
1716 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1717
1718 // Store the vector to memory (because LValue wants an address).
1719 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
1720 Builder.CreateStore(Vec, VecMem);
1721 Base = MakeAddrLValue(VecMem, E->getBase()->getType());
1722 }
1723
1724 QualType type =
1725 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
1726
1727 // Encode the element access list into a vector of unsigned indices.
1728 llvm::SmallVector<unsigned, 4> Indices;
1729 E->getEncodedElementAccess(Indices);
1730
1731 if (Base.isSimple()) {
1732 llvm::Constant *CV = GenerateConstantVector(getLLVMContext(), Indices);
1733 return LValue::MakeExtVectorElt(Base.getAddress(), CV, type);
1734 }
1735 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1736
1737 llvm::Constant *BaseElts = Base.getExtVectorElts();
1738 llvm::SmallVector<llvm::Constant *, 4> CElts;
1739
1740 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1741 if (isa<llvm::ConstantAggregateZero>(BaseElts))
1742 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
1743 else
1744 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
1745 }
1746 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
1747 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type);
1748 }
1749
EmitMemberExpr(const MemberExpr * E)1750 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
1751 bool isNonGC = false;
1752 Expr *BaseExpr = E->getBase();
1753 llvm::Value *BaseValue = NULL;
1754 Qualifiers BaseQuals;
1755
1756 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
1757 if (E->isArrow()) {
1758 BaseValue = EmitScalarExpr(BaseExpr);
1759 const PointerType *PTy =
1760 BaseExpr->getType()->getAs<PointerType>();
1761 BaseQuals = PTy->getPointeeType().getQualifiers();
1762 } else {
1763 LValue BaseLV = EmitLValue(BaseExpr);
1764 if (BaseLV.isNonGC())
1765 isNonGC = true;
1766 // FIXME: this isn't right for bitfields.
1767 BaseValue = BaseLV.getAddress();
1768 QualType BaseTy = BaseExpr->getType();
1769 BaseQuals = BaseTy.getQualifiers();
1770 }
1771
1772 NamedDecl *ND = E->getMemberDecl();
1773 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1774 LValue LV = EmitLValueForField(BaseValue, Field,
1775 BaseQuals.getCVRQualifiers());
1776 LV.setNonGC(isNonGC);
1777 setObjCGCLValueClass(getContext(), E, LV);
1778 return LV;
1779 }
1780
1781 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1782 return EmitGlobalVarDeclLValue(*this, E, VD);
1783
1784 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1785 return EmitFunctionDeclLValue(*this, E, FD);
1786
1787 assert(false && "Unhandled member declaration!");
1788 return LValue();
1789 }
1790
EmitLValueForBitfield(llvm::Value * BaseValue,const FieldDecl * Field,unsigned CVRQualifiers)1791 LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value *BaseValue,
1792 const FieldDecl *Field,
1793 unsigned CVRQualifiers) {
1794 const CGRecordLayout &RL =
1795 CGM.getTypes().getCGRecordLayout(Field->getParent());
1796 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
1797 return LValue::MakeBitfield(BaseValue, Info,
1798 Field->getType().withCVRQualifiers(CVRQualifiers));
1799 }
1800
1801 /// EmitLValueForAnonRecordField - Given that the field is a member of
1802 /// an anonymous struct or union buried inside a record, and given
1803 /// that the base value is a pointer to the enclosing record, derive
1804 /// an lvalue for the ultimate field.
EmitLValueForAnonRecordField(llvm::Value * BaseValue,const IndirectFieldDecl * Field,unsigned CVRQualifiers)1805 LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue,
1806 const IndirectFieldDecl *Field,
1807 unsigned CVRQualifiers) {
1808 IndirectFieldDecl::chain_iterator I = Field->chain_begin(),
1809 IEnd = Field->chain_end();
1810 while (true) {
1811 LValue LV = EmitLValueForField(BaseValue, cast<FieldDecl>(*I),
1812 CVRQualifiers);
1813 if (++I == IEnd) return LV;
1814
1815 assert(LV.isSimple());
1816 BaseValue = LV.getAddress();
1817 CVRQualifiers |= LV.getVRQualifiers();
1818 }
1819 }
1820
EmitLValueForField(llvm::Value * baseAddr,const FieldDecl * field,unsigned cvr)1821 LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr,
1822 const FieldDecl *field,
1823 unsigned cvr) {
1824 if (field->isBitField())
1825 return EmitLValueForBitfield(baseAddr, field, cvr);
1826
1827 const RecordDecl *rec = field->getParent();
1828 QualType type = field->getType();
1829
1830 bool mayAlias = rec->hasAttr<MayAliasAttr>();
1831
1832 llvm::Value *addr = baseAddr;
1833 if (rec->isUnion()) {
1834 // For unions, there is no pointer adjustment.
1835 assert(!type->isReferenceType() && "union has reference member");
1836 } else {
1837 // For structs, we GEP to the field that the record layout suggests.
1838 unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
1839 addr = Builder.CreateStructGEP(addr, idx, field->getName());
1840
1841 // If this is a reference field, load the reference right now.
1842 if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
1843 llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
1844 if (cvr & Qualifiers::Volatile) load->setVolatile(true);
1845
1846 if (CGM.shouldUseTBAA()) {
1847 llvm::MDNode *tbaa;
1848 if (mayAlias)
1849 tbaa = CGM.getTBAAInfo(getContext().CharTy);
1850 else
1851 tbaa = CGM.getTBAAInfo(type);
1852 CGM.DecorateInstruction(load, tbaa);
1853 }
1854
1855 addr = load;
1856 mayAlias = false;
1857 type = refType->getPointeeType();
1858 cvr = 0; // qualifiers don't recursively apply to referencee
1859 }
1860 }
1861
1862 // Make sure that the address is pointing to the right type. This is critical
1863 // for both unions and structs. A union needs a bitcast, a struct element
1864 // will need a bitcast if the LLVM type laid out doesn't match the desired
1865 // type.
1866 addr = EmitBitCastOfLValueToProperType(*this, addr,
1867 CGM.getTypes().ConvertTypeForMem(type),
1868 field->getName());
1869
1870 unsigned alignment = getContext().getDeclAlign(field).getQuantity();
1871 LValue LV = MakeAddrLValue(addr, type, alignment);
1872 LV.getQuals().addCVRQualifiers(cvr);
1873
1874 // __weak attribute on a field is ignored.
1875 if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
1876 LV.getQuals().removeObjCGCAttr();
1877
1878 // Fields of may_alias structs act like 'char' for TBAA purposes.
1879 // FIXME: this should get propagated down through anonymous structs
1880 // and unions.
1881 if (mayAlias && LV.getTBAAInfo())
1882 LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
1883
1884 return LV;
1885 }
1886
1887 LValue
EmitLValueForFieldInitialization(llvm::Value * BaseValue,const FieldDecl * Field,unsigned CVRQualifiers)1888 CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue,
1889 const FieldDecl *Field,
1890 unsigned CVRQualifiers) {
1891 QualType FieldType = Field->getType();
1892
1893 if (!FieldType->isReferenceType())
1894 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1895
1896 const CGRecordLayout &RL =
1897 CGM.getTypes().getCGRecordLayout(Field->getParent());
1898 unsigned idx = RL.getLLVMFieldNo(Field);
1899 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1900 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1901
1902
1903 // Make sure that the address is pointing to the right type. This is critical
1904 // for both unions and structs. A union needs a bitcast, a struct element
1905 // will need a bitcast if the LLVM type laid out doesn't match the desired
1906 // type.
1907 llvm::Type *llvmType = ConvertTypeForMem(FieldType);
1908 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1909 V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS));
1910
1911 unsigned Alignment = getContext().getDeclAlign(Field).getQuantity();
1912 return MakeAddrLValue(V, FieldType, Alignment);
1913 }
1914
EmitCompoundLiteralLValue(const CompoundLiteralExpr * E)1915 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
1916 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
1917 const Expr *InitExpr = E->getInitializer();
1918 LValue Result = MakeAddrLValue(DeclPtr, E->getType());
1919
1920 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
1921 /*Init*/ true);
1922
1923 return Result;
1924 }
1925
1926 LValue CodeGenFunction::
EmitConditionalOperatorLValue(const AbstractConditionalOperator * expr)1927 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
1928 if (!expr->isGLValue()) {
1929 // ?: here should be an aggregate.
1930 assert((hasAggregateLLVMType(expr->getType()) &&
1931 !expr->getType()->isAnyComplexType()) &&
1932 "Unexpected conditional operator!");
1933 return EmitAggExprToLValue(expr);
1934 }
1935
1936 const Expr *condExpr = expr->getCond();
1937 bool CondExprBool;
1938 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
1939 const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
1940 if (!CondExprBool) std::swap(live, dead);
1941
1942 if (!ContainsLabel(dead))
1943 return EmitLValue(live);
1944 }
1945
1946 OpaqueValueMapping binding(*this, expr);
1947
1948 llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
1949 llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
1950 llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
1951
1952 ConditionalEvaluation eval(*this);
1953 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock);
1954
1955 // Any temporaries created here are conditional.
1956 EmitBlock(lhsBlock);
1957 eval.begin(*this);
1958 LValue lhs = EmitLValue(expr->getTrueExpr());
1959 eval.end(*this);
1960
1961 if (!lhs.isSimple())
1962 return EmitUnsupportedLValue(expr, "conditional operator");
1963
1964 lhsBlock = Builder.GetInsertBlock();
1965 Builder.CreateBr(contBlock);
1966
1967 // Any temporaries created here are conditional.
1968 EmitBlock(rhsBlock);
1969 eval.begin(*this);
1970 LValue rhs = EmitLValue(expr->getFalseExpr());
1971 eval.end(*this);
1972 if (!rhs.isSimple())
1973 return EmitUnsupportedLValue(expr, "conditional operator");
1974 rhsBlock = Builder.GetInsertBlock();
1975
1976 EmitBlock(contBlock);
1977
1978 llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2,
1979 "cond-lvalue");
1980 phi->addIncoming(lhs.getAddress(), lhsBlock);
1981 phi->addIncoming(rhs.getAddress(), rhsBlock);
1982 return MakeAddrLValue(phi, expr->getType());
1983 }
1984
1985 /// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1986 /// If the cast is a dynamic_cast, we can have the usual lvalue result,
1987 /// otherwise if a cast is needed by the code generator in an lvalue context,
1988 /// then it must mean that we need the address of an aggregate in order to
1989 /// access one of its fields. This can happen for all the reasons that casts
1990 /// are permitted with aggregate result, including noop aggregate casts, and
1991 /// cast from scalar to union.
EmitCastLValue(const CastExpr * E)1992 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1993 switch (E->getCastKind()) {
1994 case CK_ToVoid:
1995 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1996
1997 case CK_Dependent:
1998 llvm_unreachable("dependent cast kind in IR gen!");
1999
2000 case CK_GetObjCProperty: {
2001 LValue LV = EmitLValue(E->getSubExpr());
2002 assert(LV.isPropertyRef());
2003 RValue RV = EmitLoadOfPropertyRefLValue(LV);
2004
2005 // Property is an aggregate r-value.
2006 if (RV.isAggregate()) {
2007 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2008 }
2009
2010 // Implicit property returns an l-value.
2011 assert(RV.isScalar());
2012 return MakeAddrLValue(RV.getScalarVal(), E->getSubExpr()->getType());
2013 }
2014
2015 case CK_NoOp:
2016 case CK_LValueToRValue:
2017 if (!E->getSubExpr()->Classify(getContext()).isPRValue()
2018 || E->getType()->isRecordType())
2019 return EmitLValue(E->getSubExpr());
2020 // Fall through to synthesize a temporary.
2021
2022 case CK_BitCast:
2023 case CK_ArrayToPointerDecay:
2024 case CK_FunctionToPointerDecay:
2025 case CK_NullToMemberPointer:
2026 case CK_NullToPointer:
2027 case CK_IntegralToPointer:
2028 case CK_PointerToIntegral:
2029 case CK_PointerToBoolean:
2030 case CK_VectorSplat:
2031 case CK_IntegralCast:
2032 case CK_IntegralToBoolean:
2033 case CK_IntegralToFloating:
2034 case CK_FloatingToIntegral:
2035 case CK_FloatingToBoolean:
2036 case CK_FloatingCast:
2037 case CK_FloatingRealToComplex:
2038 case CK_FloatingComplexToReal:
2039 case CK_FloatingComplexToBoolean:
2040 case CK_FloatingComplexCast:
2041 case CK_FloatingComplexToIntegralComplex:
2042 case CK_IntegralRealToComplex:
2043 case CK_IntegralComplexToReal:
2044 case CK_IntegralComplexToBoolean:
2045 case CK_IntegralComplexCast:
2046 case CK_IntegralComplexToFloatingComplex:
2047 case CK_DerivedToBaseMemberPointer:
2048 case CK_BaseToDerivedMemberPointer:
2049 case CK_MemberPointerToBoolean:
2050 case CK_AnyPointerToBlockPointerCast:
2051 case CK_ObjCProduceObject:
2052 case CK_ObjCConsumeObject:
2053 case CK_ObjCReclaimReturnedObject: {
2054 // These casts only produce lvalues when we're binding a reference to a
2055 // temporary realized from a (converted) pure rvalue. Emit the expression
2056 // as a value, copy it into a temporary, and return an lvalue referring to
2057 // that temporary.
2058 llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp");
2059 EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false);
2060 return MakeAddrLValue(V, E->getType());
2061 }
2062
2063 case CK_Dynamic: {
2064 LValue LV = EmitLValue(E->getSubExpr());
2065 llvm::Value *V = LV.getAddress();
2066 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
2067 return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2068 }
2069
2070 case CK_ConstructorConversion:
2071 case CK_UserDefinedConversion:
2072 case CK_AnyPointerToObjCPointerCast:
2073 return EmitLValue(E->getSubExpr());
2074
2075 case CK_UncheckedDerivedToBase:
2076 case CK_DerivedToBase: {
2077 const RecordType *DerivedClassTy =
2078 E->getSubExpr()->getType()->getAs<RecordType>();
2079 CXXRecordDecl *DerivedClassDecl =
2080 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2081
2082 LValue LV = EmitLValue(E->getSubExpr());
2083 llvm::Value *This = LV.getAddress();
2084
2085 // Perform the derived-to-base conversion
2086 llvm::Value *Base =
2087 GetAddressOfBaseClass(This, DerivedClassDecl,
2088 E->path_begin(), E->path_end(),
2089 /*NullCheckValue=*/false);
2090
2091 return MakeAddrLValue(Base, E->getType());
2092 }
2093 case CK_ToUnion:
2094 return EmitAggExprToLValue(E);
2095 case CK_BaseToDerived: {
2096 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2097 CXXRecordDecl *DerivedClassDecl =
2098 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2099
2100 LValue LV = EmitLValue(E->getSubExpr());
2101
2102 // Perform the base-to-derived conversion
2103 llvm::Value *Derived =
2104 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
2105 E->path_begin(), E->path_end(),
2106 /*NullCheckValue=*/false);
2107
2108 return MakeAddrLValue(Derived, E->getType());
2109 }
2110 case CK_LValueBitCast: {
2111 // This must be a reinterpret_cast (or c-style equivalent).
2112 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
2113
2114 LValue LV = EmitLValue(E->getSubExpr());
2115 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2116 ConvertType(CE->getTypeAsWritten()));
2117 return MakeAddrLValue(V, E->getType());
2118 }
2119 case CK_ObjCObjectLValueCast: {
2120 LValue LV = EmitLValue(E->getSubExpr());
2121 QualType ToType = getContext().getLValueReferenceType(E->getType());
2122 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2123 ConvertType(ToType));
2124 return MakeAddrLValue(V, E->getType());
2125 }
2126 }
2127
2128 llvm_unreachable("Unhandled lvalue cast kind?");
2129 }
2130
EmitNullInitializationLValue(const CXXScalarValueInitExpr * E)2131 LValue CodeGenFunction::EmitNullInitializationLValue(
2132 const CXXScalarValueInitExpr *E) {
2133 QualType Ty = E->getType();
2134 LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty);
2135 EmitNullInitialization(LV.getAddress(), Ty);
2136 return LV;
2137 }
2138
EmitOpaqueValueLValue(const OpaqueValueExpr * e)2139 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
2140 assert(e->isGLValue() || e->getType()->isRecordType());
2141 return getOpaqueLValueMapping(e);
2142 }
2143
EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * E)2144 LValue CodeGenFunction::EmitMaterializeTemporaryExpr(
2145 const MaterializeTemporaryExpr *E) {
2146 RValue RV = EmitReferenceBindingToExpr(E->GetTemporaryExpr(),
2147 /*InitializedDecl=*/0);
2148 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2149 }
2150
2151
2152 //===--------------------------------------------------------------------===//
2153 // Expression Emission
2154 //===--------------------------------------------------------------------===//
2155
EmitCallExpr(const CallExpr * E,ReturnValueSlot ReturnValue)2156 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
2157 ReturnValueSlot ReturnValue) {
2158 if (CGDebugInfo *DI = getDebugInfo()) {
2159 DI->setLocation(E->getLocStart());
2160 DI->UpdateLineDirectiveRegion(Builder);
2161 DI->EmitStopPoint(Builder);
2162 }
2163
2164 // Builtins never have block type.
2165 if (E->getCallee()->getType()->isBlockPointerType())
2166 return EmitBlockCallExpr(E, ReturnValue);
2167
2168 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
2169 return EmitCXXMemberCallExpr(CE, ReturnValue);
2170
2171 const Decl *TargetDecl = 0;
2172 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
2173 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
2174 TargetDecl = DRE->getDecl();
2175 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
2176 if (unsigned builtinID = FD->getBuiltinID())
2177 return EmitBuiltinExpr(FD, builtinID, E);
2178 }
2179 }
2180
2181 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
2182 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
2183 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
2184
2185 if (const CXXPseudoDestructorExpr *PseudoDtor
2186 = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
2187 QualType DestroyedType = PseudoDtor->getDestroyedType();
2188 if (getContext().getLangOptions().ObjCAutoRefCount &&
2189 DestroyedType->isObjCLifetimeType() &&
2190 (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
2191 DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
2192 // Automatic Reference Counting:
2193 // If the pseudo-expression names a retainable object with weak or
2194 // strong lifetime, the object shall be released.
2195 Expr *BaseExpr = PseudoDtor->getBase();
2196 llvm::Value *BaseValue = NULL;
2197 Qualifiers BaseQuals;
2198
2199 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2200 if (PseudoDtor->isArrow()) {
2201 BaseValue = EmitScalarExpr(BaseExpr);
2202 const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
2203 BaseQuals = PTy->getPointeeType().getQualifiers();
2204 } else {
2205 LValue BaseLV = EmitLValue(BaseExpr);
2206 BaseValue = BaseLV.getAddress();
2207 QualType BaseTy = BaseExpr->getType();
2208 BaseQuals = BaseTy.getQualifiers();
2209 }
2210
2211 switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
2212 case Qualifiers::OCL_None:
2213 case Qualifiers::OCL_ExplicitNone:
2214 case Qualifiers::OCL_Autoreleasing:
2215 break;
2216
2217 case Qualifiers::OCL_Strong:
2218 EmitARCRelease(Builder.CreateLoad(BaseValue,
2219 PseudoDtor->getDestroyedType().isVolatileQualified()),
2220 /*precise*/ true);
2221 break;
2222
2223 case Qualifiers::OCL_Weak:
2224 EmitARCDestroyWeak(BaseValue);
2225 break;
2226 }
2227 } else {
2228 // C++ [expr.pseudo]p1:
2229 // The result shall only be used as the operand for the function call
2230 // operator (), and the result of such a call has type void. The only
2231 // effect is the evaluation of the postfix-expression before the dot or
2232 // arrow.
2233 EmitScalarExpr(E->getCallee());
2234 }
2235
2236 return RValue::get(0);
2237 }
2238
2239 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
2240 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
2241 E->arg_begin(), E->arg_end(), TargetDecl);
2242 }
2243
EmitBinaryOperatorLValue(const BinaryOperator * E)2244 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
2245 // Comma expressions just emit their LHS then their RHS as an l-value.
2246 if (E->getOpcode() == BO_Comma) {
2247 EmitIgnoredExpr(E->getLHS());
2248 EnsureInsertPoint();
2249 return EmitLValue(E->getRHS());
2250 }
2251
2252 if (E->getOpcode() == BO_PtrMemD ||
2253 E->getOpcode() == BO_PtrMemI)
2254 return EmitPointerToDataMemberBinaryExpr(E);
2255
2256 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
2257
2258 // Note that in all of these cases, __block variables need the RHS
2259 // evaluated first just in case the variable gets moved by the RHS.
2260
2261 if (!hasAggregateLLVMType(E->getType())) {
2262 switch (E->getLHS()->getType().getObjCLifetime()) {
2263 case Qualifiers::OCL_Strong:
2264 return EmitARCStoreStrong(E, /*ignored*/ false).first;
2265
2266 case Qualifiers::OCL_Autoreleasing:
2267 return EmitARCStoreAutoreleasing(E).first;
2268
2269 // No reason to do any of these differently.
2270 case Qualifiers::OCL_None:
2271 case Qualifiers::OCL_ExplicitNone:
2272 case Qualifiers::OCL_Weak:
2273 break;
2274 }
2275
2276 RValue RV = EmitAnyExpr(E->getRHS());
2277 LValue LV = EmitLValue(E->getLHS());
2278 EmitStoreThroughLValue(RV, LV);
2279 return LV;
2280 }
2281
2282 if (E->getType()->isAnyComplexType())
2283 return EmitComplexAssignmentLValue(E);
2284
2285 return EmitAggExprToLValue(E);
2286 }
2287
EmitCallExprLValue(const CallExpr * E)2288 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
2289 RValue RV = EmitCallExpr(E);
2290
2291 if (!RV.isScalar())
2292 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2293
2294 assert(E->getCallReturnType()->isReferenceType() &&
2295 "Can't have a scalar return unless the return type is a "
2296 "reference type!");
2297
2298 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2299 }
2300
EmitVAArgExprLValue(const VAArgExpr * E)2301 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
2302 // FIXME: This shouldn't require another copy.
2303 return EmitAggExprToLValue(E);
2304 }
2305
EmitCXXConstructLValue(const CXXConstructExpr * E)2306 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
2307 assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
2308 && "binding l-value to type which needs a temporary");
2309 AggValueSlot Slot = CreateAggTemp(E->getType(), "tmp");
2310 EmitCXXConstructExpr(E, Slot);
2311 return MakeAddrLValue(Slot.getAddr(), E->getType());
2312 }
2313
2314 LValue
EmitCXXTypeidLValue(const CXXTypeidExpr * E)2315 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
2316 return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
2317 }
2318
2319 LValue
EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr * E)2320 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
2321 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2322 Slot.setLifetimeExternallyManaged();
2323 EmitAggExpr(E->getSubExpr(), Slot);
2324 EmitCXXTemporary(E->getTemporary(), Slot.getAddr());
2325 return MakeAddrLValue(Slot.getAddr(), E->getType());
2326 }
2327
EmitObjCMessageExprLValue(const ObjCMessageExpr * E)2328 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
2329 RValue RV = EmitObjCMessageExpr(E);
2330
2331 if (!RV.isScalar())
2332 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2333
2334 assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
2335 "Can't have a scalar return unless the return type is a "
2336 "reference type!");
2337
2338 return MakeAddrLValue(RV.getScalarVal(), E->getType());
2339 }
2340
EmitObjCSelectorLValue(const ObjCSelectorExpr * E)2341 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
2342 llvm::Value *V =
2343 CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
2344 return MakeAddrLValue(V, E->getType());
2345 }
2346
EmitIvarOffset(const ObjCInterfaceDecl * Interface,const ObjCIvarDecl * Ivar)2347 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2348 const ObjCIvarDecl *Ivar) {
2349 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
2350 }
2351
EmitLValueForIvar(QualType ObjectTy,llvm::Value * BaseValue,const ObjCIvarDecl * Ivar,unsigned CVRQualifiers)2352 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
2353 llvm::Value *BaseValue,
2354 const ObjCIvarDecl *Ivar,
2355 unsigned CVRQualifiers) {
2356 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
2357 Ivar, CVRQualifiers);
2358 }
2359
EmitObjCIvarRefLValue(const ObjCIvarRefExpr * E)2360 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
2361 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
2362 llvm::Value *BaseValue = 0;
2363 const Expr *BaseExpr = E->getBase();
2364 Qualifiers BaseQuals;
2365 QualType ObjectTy;
2366 if (E->isArrow()) {
2367 BaseValue = EmitScalarExpr(BaseExpr);
2368 ObjectTy = BaseExpr->getType()->getPointeeType();
2369 BaseQuals = ObjectTy.getQualifiers();
2370 } else {
2371 LValue BaseLV = EmitLValue(BaseExpr);
2372 // FIXME: this isn't right for bitfields.
2373 BaseValue = BaseLV.getAddress();
2374 ObjectTy = BaseExpr->getType();
2375 BaseQuals = ObjectTy.getQualifiers();
2376 }
2377
2378 LValue LV =
2379 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
2380 BaseQuals.getCVRQualifiers());
2381 setObjCGCLValueClass(getContext(), E, LV);
2382 return LV;
2383 }
2384
EmitStmtExprLValue(const StmtExpr * E)2385 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
2386 // Can only get l-value for message expression returning aggregate type
2387 RValue RV = EmitAnyExprToTemp(E);
2388 return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2389 }
2390
EmitCall(QualType CalleeType,llvm::Value * Callee,ReturnValueSlot ReturnValue,CallExpr::const_arg_iterator ArgBeg,CallExpr::const_arg_iterator ArgEnd,const Decl * TargetDecl)2391 RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
2392 ReturnValueSlot ReturnValue,
2393 CallExpr::const_arg_iterator ArgBeg,
2394 CallExpr::const_arg_iterator ArgEnd,
2395 const Decl *TargetDecl) {
2396 // Get the actual function type. The callee type will always be a pointer to
2397 // function type or a block pointer type.
2398 assert(CalleeType->isFunctionPointerType() &&
2399 "Call must have function pointer type!");
2400
2401 CalleeType = getContext().getCanonicalType(CalleeType);
2402
2403 const FunctionType *FnType
2404 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
2405
2406 CallArgList Args;
2407 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
2408
2409 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
2410 Callee, ReturnValue, Args, TargetDecl);
2411 }
2412
2413 LValue CodeGenFunction::
EmitPointerToDataMemberBinaryExpr(const BinaryOperator * E)2414 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
2415 llvm::Value *BaseV;
2416 if (E->getOpcode() == BO_PtrMemI)
2417 BaseV = EmitScalarExpr(E->getLHS());
2418 else
2419 BaseV = EmitLValue(E->getLHS()).getAddress();
2420
2421 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
2422
2423 const MemberPointerType *MPT
2424 = E->getRHS()->getType()->getAs<MemberPointerType>();
2425
2426 llvm::Value *AddV =
2427 CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
2428
2429 return MakeAddrLValue(AddV, MPT->getPointeeType());
2430 }
2431