123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862 |
- //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Frontend/CodeGenOptions.h"
- #include "CodeGenFunction.h"
- #include "CGCXXABI.h"
- #include "CGObjCRuntime.h"
- #include "CodeGenModule.h"
- #include "CGDebugInfo.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/DeclObjC.h"
- #include "clang/AST/RecordLayout.h"
- #include "clang/AST/StmtVisitor.h"
- #include "clang/Basic/TargetInfo.h"
- #include "llvm/Constants.h"
- #include "llvm/Function.h"
- #include "llvm/GlobalVariable.h"
- #include "llvm/Intrinsics.h"
- #include "llvm/Module.h"
- #include "llvm/Support/CFG.h"
- #include "llvm/Target/TargetData.h"
- #include <cstdarg>
- using namespace clang;
- using namespace CodeGen;
- using llvm::Value;
- //===----------------------------------------------------------------------===//
- // Scalar Expression Emitter
- //===----------------------------------------------------------------------===//
- namespace {
- struct BinOpInfo {
- Value *LHS;
- Value *RHS;
- QualType Ty; // Computation Type.
- BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform
- const Expr *E; // Entire expr, for error unsupported. May not be binop.
- };
- static bool MustVisitNullValue(const Expr *E) {
- // If a null pointer expression's type is the C++0x nullptr_t, then
- // it's not necessarily a simple constant and it must be evaluated
- // for its potential side effects.
- return E->getType()->isNullPtrType();
- }
- class ScalarExprEmitter
- : public StmtVisitor<ScalarExprEmitter, Value*> {
- CodeGenFunction &CGF;
- CGBuilderTy &Builder;
- bool IgnoreResultAssign;
- llvm::LLVMContext &VMContext;
- public:
- ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
- : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
- VMContext(cgf.getLLVMContext()) {
- }
- //===--------------------------------------------------------------------===//
- // Utilities
- //===--------------------------------------------------------------------===//
- bool TestAndClearIgnoreResultAssign() {
- bool I = IgnoreResultAssign;
- IgnoreResultAssign = false;
- return I;
- }
- llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
- LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
- LValue EmitCheckedLValue(const Expr *E) { return CGF.EmitCheckedLValue(E); }
- Value *EmitLoadOfLValue(LValue LV) {
- return CGF.EmitLoadOfLValue(LV).getScalarVal();
- }
- /// EmitLoadOfLValue - Given an expression with complex type that represents a
- /// value l-value, this method emits the address of the l-value, then loads
- /// and returns the result.
- Value *EmitLoadOfLValue(const Expr *E) {
- return EmitLoadOfLValue(EmitCheckedLValue(E));
- }
- /// EmitConversionToBool - Convert the specified expression value to a
- /// boolean (i1) truth value. This is equivalent to "Val != 0".
- Value *EmitConversionToBool(Value *Src, QualType DstTy);
- /// EmitScalarConversion - Emit a conversion from the specified type to the
- /// specified destination type, both of which are LLVM scalar types.
- Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
- /// EmitComplexToScalarConversion - Emit a conversion from the specified
- /// complex type to the specified destination type, where the destination type
- /// is an LLVM scalar type.
- Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
- QualType SrcTy, QualType DstTy);
- /// EmitNullValue - Emit a value that corresponds to null for the given type.
- Value *EmitNullValue(QualType Ty);
- /// EmitFloatToBoolConversion - Perform an FP to boolean conversion.
- Value *EmitFloatToBoolConversion(Value *V) {
- // Compare against 0.0 for fp scalars.
- llvm::Value *Zero = llvm::Constant::getNullValue(V->getType());
- return Builder.CreateFCmpUNE(V, Zero, "tobool");
- }
- /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion.
- Value *EmitPointerToBoolConversion(Value *V) {
- Value *Zero = llvm::ConstantPointerNull::get(
- cast<llvm::PointerType>(V->getType()));
- return Builder.CreateICmpNE(V, Zero, "tobool");
- }
- Value *EmitIntToBoolConversion(Value *V) {
- // Because of the type rules of C, we often end up computing a
- // logical value, then zero extending it to int, then wanting it
- // as a logical value again. Optimize this common case.
- if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) {
- if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) {
- Value *Result = ZI->getOperand(0);
- // If there aren't any more uses, zap the instruction to save space.
- // Note that there can be more uses, for example if this
- // is the result of an assignment.
- if (ZI->use_empty())
- ZI->eraseFromParent();
- return Result;
- }
- }
- return Builder.CreateIsNotNull(V, "tobool");
- }
- //===--------------------------------------------------------------------===//
- // Visitor Methods
- //===--------------------------------------------------------------------===//
- Value *Visit(Expr *E) {
- return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
- }
-
- Value *VisitStmt(Stmt *S) {
- S->dump(CGF.getContext().getSourceManager());
- llvm_unreachable("Stmt can't have complex result type!");
- }
- Value *VisitExpr(Expr *S);
-
- Value *VisitParenExpr(ParenExpr *PE) {
- return Visit(PE->getSubExpr());
- }
- Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
- return Visit(E->getReplacement());
- }
- Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
- return Visit(GE->getResultExpr());
- }
- // Leaves.
- Value *VisitIntegerLiteral(const IntegerLiteral *E) {
- return Builder.getInt(E->getValue());
- }
- Value *VisitFloatingLiteral(const FloatingLiteral *E) {
- return llvm::ConstantFP::get(VMContext, E->getValue());
- }
- Value *VisitCharacterLiteral(const CharacterLiteral *E) {
- return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
- }
- Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
- return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
- }
- Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
- return EmitNullValue(E->getType());
- }
- Value *VisitGNUNullExpr(const GNUNullExpr *E) {
- return EmitNullValue(E->getType());
- }
- Value *VisitOffsetOfExpr(OffsetOfExpr *E);
- Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
- Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
- llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
- return Builder.CreateBitCast(V, ConvertType(E->getType()));
- }
- Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
- return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength());
- }
- Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) {
- return CGF.EmitPseudoObjectRValue(E).getScalarVal();
- }
- Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) {
- if (E->isGLValue())
- return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E));
- // Otherwise, assume the mapping is the scalar directly.
- return CGF.getOpaqueRValueMapping(E).getScalarVal();
- }
-
- // l-values.
- Value *VisitDeclRefExpr(DeclRefExpr *E) {
- Expr::EvalResult Result;
- if (!E->EvaluateAsRValue(Result, CGF.getContext()))
- return EmitLoadOfLValue(E);
- assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
- llvm::Constant *C;
- if (Result.Val.isInt())
- C = Builder.getInt(Result.Val.getInt());
- else if (Result.Val.isFloat())
- C = llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
- else
- return EmitLoadOfLValue(E);
- // Make sure we emit a debug reference to the global variable.
- if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
- if (!CGF.getContext().DeclMustBeEmitted(VD))
- CGF.EmitDeclRefExprDbgValue(E, C);
- } else if (isa<EnumConstantDecl>(E->getDecl())) {
- CGF.EmitDeclRefExprDbgValue(E, C);
- }
- return C;
- }
- Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
- return CGF.EmitObjCSelectorExpr(E);
- }
- Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
- return CGF.EmitObjCProtocolExpr(E);
- }
- Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
- return EmitLoadOfLValue(E);
- }
- Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
- if (E->getMethodDecl() &&
- E->getMethodDecl()->getResultType()->isReferenceType())
- return EmitLoadOfLValue(E);
- return CGF.EmitObjCMessageExpr(E).getScalarVal();
- }
- Value *VisitObjCIsaExpr(ObjCIsaExpr *E) {
- LValue LV = CGF.EmitObjCIsaExpr(E);
- Value *V = CGF.EmitLoadOfLValue(LV).getScalarVal();
- return V;
- }
- Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
- Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
- Value *VisitMemberExpr(MemberExpr *E);
- Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
- Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
- return EmitLoadOfLValue(E);
- }
- Value *VisitInitListExpr(InitListExpr *E);
- Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
- return CGF.CGM.EmitNullConstant(E->getType());
- }
- Value *VisitExplicitCastExpr(ExplicitCastExpr *E) {
- if (E->getType()->isVariablyModifiedType())
- CGF.EmitVariablyModifiedType(E->getType());
- return VisitCastExpr(E);
- }
- Value *VisitCastExpr(CastExpr *E);
- Value *VisitCallExpr(const CallExpr *E) {
- if (E->getCallReturnType()->isReferenceType())
- return EmitLoadOfLValue(E);
- return CGF.EmitCallExpr(E).getScalarVal();
- }
- Value *VisitStmtExpr(const StmtExpr *E);
- Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
- // Unary Operators.
- Value *VisitUnaryPostDec(const UnaryOperator *E) {
- LValue LV = EmitLValue(E->getSubExpr());
- return EmitScalarPrePostIncDec(E, LV, false, false);
- }
- Value *VisitUnaryPostInc(const UnaryOperator *E) {
- LValue LV = EmitLValue(E->getSubExpr());
- return EmitScalarPrePostIncDec(E, LV, true, false);
- }
- Value *VisitUnaryPreDec(const UnaryOperator *E) {
- LValue LV = EmitLValue(E->getSubExpr());
- return EmitScalarPrePostIncDec(E, LV, false, true);
- }
- Value *VisitUnaryPreInc(const UnaryOperator *E) {
- LValue LV = EmitLValue(E->getSubExpr());
- return EmitScalarPrePostIncDec(E, LV, true, true);
- }
- llvm::Value *EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
- llvm::Value *InVal,
- llvm::Value *NextVal,
- bool IsInc);
- llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
- bool isInc, bool isPre);
-
- Value *VisitUnaryAddrOf(const UnaryOperator *E) {
- if (isa<MemberPointerType>(E->getType())) // never sugared
- return CGF.CGM.getMemberPointerConstant(E);
- return EmitLValue(E->getSubExpr()).getAddress();
- }
- Value *VisitUnaryDeref(const UnaryOperator *E) {
- if (E->getType()->isVoidType())
- return Visit(E->getSubExpr()); // the actual value should be unused
- return EmitLoadOfLValue(E);
- }
- Value *VisitUnaryPlus(const UnaryOperator *E) {
- // This differs from gcc, though, most likely due to a bug in gcc.
- TestAndClearIgnoreResultAssign();
- return Visit(E->getSubExpr());
- }
- Value *VisitUnaryMinus (const UnaryOperator *E);
- Value *VisitUnaryNot (const UnaryOperator *E);
- Value *VisitUnaryLNot (const UnaryOperator *E);
- Value *VisitUnaryReal (const UnaryOperator *E);
- Value *VisitUnaryImag (const UnaryOperator *E);
- Value *VisitUnaryExtension(const UnaryOperator *E) {
- return Visit(E->getSubExpr());
- }
-
- // C++
- Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) {
- return EmitLoadOfLValue(E);
- }
-
- Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
- return Visit(DAE->getExpr());
- }
- Value *VisitCXXThisExpr(CXXThisExpr *TE) {
- return CGF.LoadCXXThis();
- }
- Value *VisitExprWithCleanups(ExprWithCleanups *E) {
- CGF.enterFullExpression(E);
- CodeGenFunction::RunCleanupsScope Scope(CGF);
- return Visit(E->getSubExpr());
- }
- Value *VisitCXXNewExpr(const CXXNewExpr *E) {
- return CGF.EmitCXXNewExpr(E);
- }
- Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
- CGF.EmitCXXDeleteExpr(E);
- return 0;
- }
- Value *VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
- return Builder.getInt1(E->getValue());
- }
- Value *VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
- return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
- }
- Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
- return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue());
- }
- Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
- return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue());
- }
- Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
- // C++ [expr.pseudo]p1:
- // The result shall only be used as the operand for the function call
- // operator (), and the result of such a call has type void. The only
- // effect is the evaluation of the postfix-expression before the dot or
- // arrow.
- CGF.EmitScalarExpr(E->getBase());
- return 0;
- }
- Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
- return EmitNullValue(E->getType());
- }
- Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
- CGF.EmitCXXThrowExpr(E);
- return 0;
- }
- Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
- return Builder.getInt1(E->getValue());
- }
- // Binary Operators.
- Value *EmitMul(const BinOpInfo &Ops) {
- if (Ops.Ty->isSignedIntegerOrEnumerationType()) {
- switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) {
- case LangOptions::SOB_Undefined:
- return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul");
- case LangOptions::SOB_Defined:
- return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
- case LangOptions::SOB_Trapping:
- return EmitOverflowCheckedBinOp(Ops);
- }
- }
-
- if (Ops.LHS->getType()->isFPOrFPVectorTy())
- return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
- return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
- }
- bool isTrapvOverflowBehavior() {
- return CGF.getContext().getLangOptions().getSignedOverflowBehavior()
- == LangOptions::SOB_Trapping;
- }
- /// Create a binary op that checks for overflow.
- /// Currently only supports +, - and *.
- Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
- // Emit the overflow BB when -ftrapv option is activated.
- void EmitOverflowBB(llvm::BasicBlock *overflowBB) {
- Builder.SetInsertPoint(overflowBB);
- llvm::Function *Trap = CGF.CGM.getIntrinsic(llvm::Intrinsic::trap);
- Builder.CreateCall(Trap);
- Builder.CreateUnreachable();
- }
- // Check for undefined division and modulus behaviors.
- void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops,
- llvm::Value *Zero,bool isDiv);
- Value *EmitDiv(const BinOpInfo &Ops);
- Value *EmitRem(const BinOpInfo &Ops);
- Value *EmitAdd(const BinOpInfo &Ops);
- Value *EmitSub(const BinOpInfo &Ops);
- Value *EmitShl(const BinOpInfo &Ops);
- Value *EmitShr(const BinOpInfo &Ops);
- Value *EmitAnd(const BinOpInfo &Ops) {
- return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
- }
- Value *EmitXor(const BinOpInfo &Ops) {
- return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
- }
- Value *EmitOr (const BinOpInfo &Ops) {
- return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
- }
- BinOpInfo EmitBinOps(const BinaryOperator *E);
- LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
- Value *(ScalarExprEmitter::*F)(const BinOpInfo &),
- Value *&Result);
- Value *EmitCompoundAssign(const CompoundAssignOperator *E,
- Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
- // Binary operators and binary compound assignment operators.
- #define HANDLEBINOP(OP) \
- Value *VisitBin ## OP(const BinaryOperator *E) { \
- return Emit ## OP(EmitBinOps(E)); \
- } \
- Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
- return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
- }
- HANDLEBINOP(Mul)
- HANDLEBINOP(Div)
- HANDLEBINOP(Rem)
- HANDLEBINOP(Add)
- HANDLEBINOP(Sub)
- HANDLEBINOP(Shl)
- HANDLEBINOP(Shr)
- HANDLEBINOP(And)
- HANDLEBINOP(Xor)
- HANDLEBINOP(Or)
- #undef HANDLEBINOP
- // Comparisons.
- Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
- unsigned SICmpOpc, unsigned FCmpOpc);
- #define VISITCOMP(CODE, UI, SI, FP) \
- Value *VisitBin##CODE(const BinaryOperator *E) { \
- return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
- llvm::FCmpInst::FP); }
- VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT)
- VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT)
- VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE)
- VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE)
- VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ)
- VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE)
- #undef VISITCOMP
- Value *VisitBinAssign (const BinaryOperator *E);
- Value *VisitBinLAnd (const BinaryOperator *E);
- Value *VisitBinLOr (const BinaryOperator *E);
- Value *VisitBinComma (const BinaryOperator *E);
- Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); }
- Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); }
- // Other Operators.
- Value *VisitBlockExpr(const BlockExpr *BE);
- Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *);
- Value *VisitChooseExpr(ChooseExpr *CE);
- Value *VisitVAArgExpr(VAArgExpr *VE);
- Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
- return CGF.EmitObjCStringLiteral(E);
- }
- Value *VisitAsTypeExpr(AsTypeExpr *CE);
- Value *VisitAtomicExpr(AtomicExpr *AE);
- };
- } // end anonymous namespace.
- //===----------------------------------------------------------------------===//
- // Utilities
- //===----------------------------------------------------------------------===//
- /// EmitConversionToBool - Convert the specified expression value to a
- /// boolean (i1) truth value. This is equivalent to "Val != 0".
- Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
- assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs");
- if (SrcType->isRealFloatingType())
- return EmitFloatToBoolConversion(Src);
- if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType))
- return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT);
- assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
- "Unknown scalar type to convert");
- if (isa<llvm::IntegerType>(Src->getType()))
- return EmitIntToBoolConversion(Src);
- assert(isa<llvm::PointerType>(Src->getType()));
- return EmitPointerToBoolConversion(Src);
- }
- /// EmitScalarConversion - Emit a conversion from the specified type to the
- /// specified destination type, both of which are LLVM scalar types.
- Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
- QualType DstType) {
- SrcType = CGF.getContext().getCanonicalType(SrcType);
- DstType = CGF.getContext().getCanonicalType(DstType);
- if (SrcType == DstType) return Src;
- if (DstType->isVoidType()) return 0;
- llvm::Type *SrcTy = Src->getType();
- // Floating casts might be a bit special: if we're doing casts to / from half
- // FP, we should go via special intrinsics.
- if (SrcType->isHalfType()) {
- Src = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16), Src);
- SrcType = CGF.getContext().FloatTy;
- SrcTy = CGF.FloatTy;
- }
- // Handle conversions to bool first, they are special: comparisons against 0.
- if (DstType->isBooleanType())
- return EmitConversionToBool(Src, SrcType);
- llvm::Type *DstTy = ConvertType(DstType);
- // Ignore conversions like int -> uint.
- if (SrcTy == DstTy)
- return Src;
- // Handle pointer conversions next: pointers can only be converted to/from
- // other pointers and integers. Check for pointer types in terms of LLVM, as
- // some native types (like Obj-C id) may map to a pointer type.
- if (isa<llvm::PointerType>(DstTy)) {
- // The source value may be an integer, or a pointer.
- if (isa<llvm::PointerType>(SrcTy))
- return Builder.CreateBitCast(Src, DstTy, "conv");
- assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
- // First, convert to the correct width so that we control the kind of
- // extension.
- llvm::Type *MiddleTy = CGF.IntPtrTy;
- bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
- llvm::Value* IntResult =
- Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
- // Then, cast to pointer.
- return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
- }
- if (isa<llvm::PointerType>(SrcTy)) {
- // Must be an ptr to int cast.
- assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
- return Builder.CreatePtrToInt(Src, DstTy, "conv");
- }
- // A scalar can be splatted to an extended vector of the same element type
- if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
- // Cast the scalar to element type
- QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType();
- llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
- // Insert the element in element zero of an undef vector
- llvm::Value *UnV = llvm::UndefValue::get(DstTy);
- llvm::Value *Idx = Builder.getInt32(0);
- UnV = Builder.CreateInsertElement(UnV, Elt, Idx);
- // Splat the element across to all elements
- unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
- llvm::Constant *Mask = llvm::ConstantVector::getSplat(NumElements,
- Builder.getInt32(0));
- llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
- return Yay;
- }
- // Allow bitcast from vector to integer/fp of the same size.
- if (isa<llvm::VectorType>(SrcTy) ||
- isa<llvm::VectorType>(DstTy))
- return Builder.CreateBitCast(Src, DstTy, "conv");
- // Finally, we have the arithmetic types: real int/float.
- Value *Res = NULL;
- llvm::Type *ResTy = DstTy;
- // Cast to half via float
- if (DstType->isHalfType())
- DstTy = CGF.FloatTy;
- if (isa<llvm::IntegerType>(SrcTy)) {
- bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
- if (isa<llvm::IntegerType>(DstTy))
- Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
- else if (InputSigned)
- Res = Builder.CreateSIToFP(Src, DstTy, "conv");
- else
- Res = Builder.CreateUIToFP(Src, DstTy, "conv");
- } else if (isa<llvm::IntegerType>(DstTy)) {
- assert(SrcTy->isFloatingPointTy() && "Unknown real conversion");
- if (DstType->isSignedIntegerOrEnumerationType())
- Res = Builder.CreateFPToSI(Src, DstTy, "conv");
- else
- Res = Builder.CreateFPToUI(Src, DstTy, "conv");
- } else {
- assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() &&
- "Unknown real conversion");
- if (DstTy->getTypeID() < SrcTy->getTypeID())
- Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
- else
- Res = Builder.CreateFPExt(Src, DstTy, "conv");
- }
- if (DstTy != ResTy) {
- assert(ResTy->isIntegerTy(16) && "Only half FP requires extra conversion");
- Res = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16), Res);
- }
- return Res;
- }
- /// EmitComplexToScalarConversion - Emit a conversion from the specified complex
- /// type to the specified destination type, where the destination type is an
- /// LLVM scalar type.
- Value *ScalarExprEmitter::
- EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
- QualType SrcTy, QualType DstTy) {
- // Get the source element type.
- SrcTy = SrcTy->getAs<ComplexType>()->getElementType();
- // Handle conversions to bool first, they are special: comparisons against 0.
- if (DstTy->isBooleanType()) {
- // Complex != 0 -> (Real != 0) | (Imag != 0)
- Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
- Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
- return Builder.CreateOr(Src.first, Src.second, "tobool");
- }
- // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
- // the imaginary part of the complex value is discarded and the value of the
- // real part is converted according to the conversion rules for the
- // corresponding real type.
- return EmitScalarConversion(Src.first, SrcTy, DstTy);
- }
- Value *ScalarExprEmitter::EmitNullValue(QualType Ty) {
- if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
- return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
- return llvm::Constant::getNullValue(ConvertType(Ty));
- }
- //===----------------------------------------------------------------------===//
- // Visitor Methods
- //===----------------------------------------------------------------------===//
- Value *ScalarExprEmitter::VisitExpr(Expr *E) {
- CGF.ErrorUnsupported(E, "scalar expression");
- if (E->getType()->isVoidType())
- return 0;
- return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
- }
- Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
- // Vector Mask Case
- if (E->getNumSubExprs() == 2 ||
- (E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) {
- Value *LHS = CGF.EmitScalarExpr(E->getExpr(0));
- Value *RHS = CGF.EmitScalarExpr(E->getExpr(1));
- Value *Mask;
-
- llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType());
- unsigned LHSElts = LTy->getNumElements();
- if (E->getNumSubExprs() == 3) {
- Mask = CGF.EmitScalarExpr(E->getExpr(2));
-
- // Shuffle LHS & RHS into one input vector.
- SmallVector<llvm::Constant*, 32> concat;
- for (unsigned i = 0; i != LHSElts; ++i) {
- concat.push_back(Builder.getInt32(2*i));
- concat.push_back(Builder.getInt32(2*i+1));
- }
-
- Value* CV = llvm::ConstantVector::get(concat);
- LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat");
- LHSElts *= 2;
- } else {
- Mask = RHS;
- }
-
- llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType());
- llvm::Constant* EltMask;
-
- // Treat vec3 like vec4.
- if ((LHSElts == 6) && (E->getNumSubExprs() == 3))
- EltMask = llvm::ConstantInt::get(MTy->getElementType(),
- (1 << llvm::Log2_32(LHSElts+2))-1);
- else if ((LHSElts == 3) && (E->getNumSubExprs() == 2))
- EltMask = llvm::ConstantInt::get(MTy->getElementType(),
- (1 << llvm::Log2_32(LHSElts+1))-1);
- else
- EltMask = llvm::ConstantInt::get(MTy->getElementType(),
- (1 << llvm::Log2_32(LHSElts))-1);
-
- // Mask off the high bits of each shuffle index.
- Value *MaskBits = llvm::ConstantVector::getSplat(MTy->getNumElements(),
- EltMask);
- Mask = Builder.CreateAnd(Mask, MaskBits, "mask");
-
- // newv = undef
- // mask = mask & maskbits
- // for each elt
- // n = extract mask i
- // x = extract val n
- // newv = insert newv, x, i
- llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(),
- MTy->getNumElements());
- Value* NewV = llvm::UndefValue::get(RTy);
- for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) {
- Value *Indx = Builder.getInt32(i);
- Indx = Builder.CreateExtractElement(Mask, Indx, "shuf_idx");
- Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext");
-
- // Handle vec3 special since the index will be off by one for the RHS.
- if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) {
- Value *cmpIndx, *newIndx;
- cmpIndx = Builder.CreateICmpUGT(Indx, Builder.getInt32(3),
- "cmp_shuf_idx");
- newIndx = Builder.CreateSub(Indx, Builder.getInt32(1), "shuf_idx_adj");
- Indx = Builder.CreateSelect(cmpIndx, newIndx, Indx, "sel_shuf_idx");
- }
- Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt");
- NewV = Builder.CreateInsertElement(NewV, VExt, Indx, "shuf_ins");
- }
- return NewV;
- }
-
- Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
- Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
-
- // Handle vec3 special since the index will be off by one for the RHS.
- llvm::VectorType *VTy = cast<llvm::VectorType>(V1->getType());
- SmallVector<llvm::Constant*, 32> indices;
- for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
- unsigned Idx = E->getShuffleMaskIdx(CGF.getContext(), i-2);
- if (VTy->getNumElements() == 3 && Idx > 3)
- Idx -= 1;
- indices.push_back(Builder.getInt32(Idx));
- }
- Value *SV = llvm::ConstantVector::get(indices);
- return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
- }
- Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
- llvm::APSInt Value;
- if (E->EvaluateAsInt(Value, CGF.getContext(), Expr::SE_AllowSideEffects)) {
- if (E->isArrow())
- CGF.EmitScalarExpr(E->getBase());
- else
- EmitLValue(E->getBase());
- return Builder.getInt(Value);
- }
- // Emit debug info for aggregate now, if it was delayed to reduce
- // debug info size.
- CGDebugInfo *DI = CGF.getDebugInfo();
- if (DI && CGF.CGM.getCodeGenOpts().LimitDebugInfo) {
- QualType PQTy = E->getBase()->IgnoreParenImpCasts()->getType();
- if (const PointerType * PTy = dyn_cast<PointerType>(PQTy))
- if (FieldDecl *M = dyn_cast<FieldDecl>(E->getMemberDecl()))
- DI->getOrCreateRecordType(PTy->getPointeeType(),
- M->getParent()->getLocation());
- }
- return EmitLoadOfLValue(E);
- }
- Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
- TestAndClearIgnoreResultAssign();
- // Emit subscript expressions in rvalue context's. For most cases, this just
- // loads the lvalue formed by the subscript expr. However, we have to be
- // careful, because the base of a vector subscript is occasionally an rvalue,
- // so we can't get it as an lvalue.
- if (!E->getBase()->getType()->isVectorType())
- return EmitLoadOfLValue(E);
- // Handle the vector case. The base must be a vector, the index must be an
- // integer value.
- Value *Base = Visit(E->getBase());
- Value *Idx = Visit(E->getIdx());
- bool IdxSigned = E->getIdx()->getType()->isSignedIntegerOrEnumerationType();
- Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vecidxcast");
- return Builder.CreateExtractElement(Base, Idx, "vecext");
- }
- static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
- unsigned Off, llvm::Type *I32Ty) {
- int MV = SVI->getMaskValue(Idx);
- if (MV == -1)
- return llvm::UndefValue::get(I32Ty);
- return llvm::ConstantInt::get(I32Ty, Off+MV);
- }
- Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
- bool Ignore = TestAndClearIgnoreResultAssign();
- (void)Ignore;
- assert (Ignore == false && "init list ignored");
- unsigned NumInitElements = E->getNumInits();
-
- if (E->hadArrayRangeDesignator())
- CGF.ErrorUnsupported(E, "GNU array range designator extension");
-
- llvm::VectorType *VType =
- dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
-
- if (!VType) {
- if (NumInitElements == 0) {
- // C++11 value-initialization for the scalar.
- return EmitNullValue(E->getType());
- }
- // We have a scalar in braces. Just use the first element.
- return Visit(E->getInit(0));
- }
-
- unsigned ResElts = VType->getNumElements();
-
- // Loop over initializers collecting the Value for each, and remembering
- // whether the source was swizzle (ExtVectorElementExpr). This will allow
- // us to fold the shuffle for the swizzle into the shuffle for the vector
- // initializer, since LLVM optimizers generally do not want to touch
- // shuffles.
- unsigned CurIdx = 0;
- bool VIsUndefShuffle = false;
- llvm::Value *V = llvm::UndefValue::get(VType);
- for (unsigned i = 0; i != NumInitElements; ++i) {
- Expr *IE = E->getInit(i);
- Value *Init = Visit(IE);
- SmallVector<llvm::Constant*, 16> Args;
-
- llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
-
- // Handle scalar elements. If the scalar initializer is actually one
- // element of a different vector of the same width, use shuffle instead of
- // extract+insert.
- if (!VVT) {
- if (isa<ExtVectorElementExpr>(IE)) {
- llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
- if (EI->getVectorOperandType()->getNumElements() == ResElts) {
- llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
- Value *LHS = 0, *RHS = 0;
- if (CurIdx == 0) {
- // insert into undef -> shuffle (src, undef)
- Args.push_back(C);
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- LHS = EI->getVectorOperand();
- RHS = V;
- VIsUndefShuffle = true;
- } else if (VIsUndefShuffle) {
- // insert into undefshuffle && size match -> shuffle (v, src)
- llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
- for (unsigned j = 0; j != CurIdx; ++j)
- Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty));
- Args.push_back(Builder.getInt32(ResElts + C->getZExtValue()));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
- RHS = EI->getVectorOperand();
- VIsUndefShuffle = false;
- }
- if (!Args.empty()) {
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- V = Builder.CreateShuffleVector(LHS, RHS, Mask);
- ++CurIdx;
- continue;
- }
- }
- }
- V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx),
- "vecinit");
- VIsUndefShuffle = false;
- ++CurIdx;
- continue;
- }
-
- unsigned InitElts = VVT->getNumElements();
- // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
- // input is the same width as the vector being constructed, generate an
- // optimized shuffle of the swizzle input into the result.
- unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
- if (isa<ExtVectorElementExpr>(IE)) {
- llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
- Value *SVOp = SVI->getOperand(0);
- llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
-
- if (OpTy->getNumElements() == ResElts) {
- for (unsigned j = 0; j != CurIdx; ++j) {
- // If the current vector initializer is a shuffle with undef, merge
- // this shuffle directly into it.
- if (VIsUndefShuffle) {
- Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
- CGF.Int32Ty));
- } else {
- Args.push_back(Builder.getInt32(j));
- }
- }
- for (unsigned j = 0, je = InitElts; j != je; ++j)
- Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- if (VIsUndefShuffle)
- V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
- Init = SVOp;
- }
- }
- // Extend init to result vector length, and then shuffle its contribution
- // to the vector initializer into V.
- if (Args.empty()) {
- for (unsigned j = 0; j != InitElts; ++j)
- Args.push_back(Builder.getInt32(j));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
- Mask, "vext");
- Args.clear();
- for (unsigned j = 0; j != CurIdx; ++j)
- Args.push_back(Builder.getInt32(j));
- for (unsigned j = 0; j != InitElts; ++j)
- Args.push_back(Builder.getInt32(j+Offset));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- }
- // If V is undef, make sure it ends up on the RHS of the shuffle to aid
- // merging subsequent shuffles into this one.
- if (CurIdx == 0)
- std::swap(V, Init);
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
- VIsUndefShuffle = isa<llvm::UndefValue>(Init);
- CurIdx += InitElts;
- }
-
- // FIXME: evaluate codegen vs. shuffling against constant null vector.
- // Emit remaining default initializers.
- llvm::Type *EltTy = VType->getElementType();
-
- // Emit remaining default initializers
- for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
- Value *Idx = Builder.getInt32(CurIdx);
- llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
- V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
- }
- return V;
- }
- static bool ShouldNullCheckClassCastValue(const CastExpr *CE) {
- const Expr *E = CE->getSubExpr();
- if (CE->getCastKind() == CK_UncheckedDerivedToBase)
- return false;
-
- if (isa<CXXThisExpr>(E)) {
- // We always assume that 'this' is never null.
- return false;
- }
-
- if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
- // And that glvalue casts are never null.
- if (ICE->getValueKind() != VK_RValue)
- return false;
- }
- return true;
- }
- // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
- // have to handle a more broad range of conversions than explicit casts, as they
- // handle things like function to ptr-to-function decay etc.
- Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
- Expr *E = CE->getSubExpr();
- QualType DestTy = CE->getType();
- CastKind Kind = CE->getCastKind();
-
- if (!DestTy->isVoidType())
- TestAndClearIgnoreResultAssign();
- // Since almost all cast kinds apply to scalars, this switch doesn't have
- // a default case, so the compiler will warn on a missing case. The cases
- // are in the same order as in the CastKind enum.
- switch (Kind) {
- case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
-
- case CK_LValueBitCast:
- case CK_ObjCObjectLValueCast: {
- Value *V = EmitLValue(E).getAddress();
- V = Builder.CreateBitCast(V,
- ConvertType(CGF.getContext().getPointerType(DestTy)));
- return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(V, DestTy));
- }
- case CK_CPointerToObjCPointerCast:
- case CK_BlockPointerToObjCPointerCast:
- case CK_AnyPointerToBlockPointerCast:
- case CK_BitCast: {
- Value *Src = Visit(const_cast<Expr*>(E));
- return Builder.CreateBitCast(Src, ConvertType(DestTy));
- }
- case CK_AtomicToNonAtomic:
- case CK_NonAtomicToAtomic:
- case CK_NoOp:
- case CK_UserDefinedConversion:
- return Visit(const_cast<Expr*>(E));
- case CK_BaseToDerived: {
- const CXXRecordDecl *DerivedClassDecl =
- DestTy->getCXXRecordDeclForPointerType();
-
- return CGF.GetAddressOfDerivedClass(Visit(E), DerivedClassDecl,
- CE->path_begin(), CE->path_end(),
- ShouldNullCheckClassCastValue(CE));
- }
- case CK_UncheckedDerivedToBase:
- case CK_DerivedToBase: {
- const RecordType *DerivedClassTy =
- E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
- CXXRecordDecl *DerivedClassDecl =
- cast<CXXRecordDecl>(DerivedClassTy->getDecl());
- return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl,
- CE->path_begin(), CE->path_end(),
- ShouldNullCheckClassCastValue(CE));
- }
- case CK_Dynamic: {
- Value *V = Visit(const_cast<Expr*>(E));
- const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
- return CGF.EmitDynamicCast(V, DCE);
- }
- case CK_ArrayToPointerDecay: {
- assert(E->getType()->isArrayType() &&
- "Array to pointer decay must have array source type!");
- Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
- // Note that VLA pointers are always decayed, so we don't need to do
- // anything here.
- if (!E->getType()->isVariableArrayType()) {
- assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
- assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
- ->getElementType()) &&
- "Expected pointer to array");
- V = Builder.CreateStructGEP(V, 0, "arraydecay");
- }
- // Make sure the array decay ends up being the right type. This matters if
- // the array type was of an incomplete type.
- return CGF.Builder.CreateBitCast(V, ConvertType(CE->getType()));
- }
- case CK_FunctionToPointerDecay:
- return EmitLValue(E).getAddress();
- case CK_NullToPointer:
- if (MustVisitNullValue(E))
- (void) Visit(E);
- return llvm::ConstantPointerNull::get(
- cast<llvm::PointerType>(ConvertType(DestTy)));
- case CK_NullToMemberPointer: {
- if (MustVisitNullValue(E))
- (void) Visit(E);
- const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>();
- return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
- }
- case CK_ReinterpretMemberPointer:
- case CK_BaseToDerivedMemberPointer:
- case CK_DerivedToBaseMemberPointer: {
- Value *Src = Visit(E);
-
- // Note that the AST doesn't distinguish between checked and
- // unchecked member pointer conversions, so we always have to
- // implement checked conversions here. This is inefficient when
- // actual control flow may be required in order to perform the
- // check, which it is for data member pointers (but not member
- // function pointers on Itanium and ARM).
- return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src);
- }
- case CK_ARCProduceObject:
- return CGF.EmitARCRetainScalarExpr(E);
- case CK_ARCConsumeObject:
- return CGF.EmitObjCConsumeObject(E->getType(), Visit(E));
- case CK_ARCReclaimReturnedObject: {
- llvm::Value *value = Visit(E);
- value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
- return CGF.EmitObjCConsumeObject(E->getType(), value);
- }
- case CK_ARCExtendBlockObject:
- return CGF.EmitARCExtendBlockObject(E);
- case CK_FloatingRealToComplex:
- case CK_FloatingComplexCast:
- case CK_IntegralRealToComplex:
- case CK_IntegralComplexCast:
- case CK_IntegralComplexToFloatingComplex:
- case CK_FloatingComplexToIntegralComplex:
- case CK_ConstructorConversion:
- case CK_ToUnion:
- llvm_unreachable("scalar cast to non-scalar value");
- case CK_LValueToRValue:
- assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy));
- assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!");
- return Visit(const_cast<Expr*>(E));
- case CK_IntegralToPointer: {
- Value *Src = Visit(const_cast<Expr*>(E));
- // First, convert to the correct width so that we control the kind of
- // extension.
- llvm::Type *MiddleTy = CGF.IntPtrTy;
- bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType();
- llvm::Value* IntResult =
- Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
- return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
- }
- case CK_PointerToIntegral:
- assert(!DestTy->isBooleanType() && "bool should use PointerToBool");
- return Builder.CreatePtrToInt(Visit(E), ConvertType(DestTy));
- case CK_ToVoid: {
- CGF.EmitIgnoredExpr(E);
- return 0;
- }
- case CK_VectorSplat: {
- llvm::Type *DstTy = ConvertType(DestTy);
- Value *Elt = Visit(const_cast<Expr*>(E));
- Elt = EmitScalarConversion(Elt, E->getType(),
- DestTy->getAs<VectorType>()->getElementType());
- // Insert the element in element zero of an undef vector
- llvm::Value *UnV = llvm::UndefValue::get(DstTy);
- llvm::Value *Idx = Builder.getInt32(0);
- UnV = Builder.CreateInsertElement(UnV, Elt, Idx);
- // Splat the element across to all elements
- unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
- llvm::Constant *Zero = Builder.getInt32(0);
- llvm::Constant *Mask = llvm::ConstantVector::getSplat(NumElements, Zero);
- llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
- return Yay;
- }
- case CK_IntegralCast:
- case CK_IntegralToFloating:
- case CK_FloatingToIntegral:
- case CK_FloatingCast:
- return EmitScalarConversion(Visit(E), E->getType(), DestTy);
- case CK_IntegralToBoolean:
- return EmitIntToBoolConversion(Visit(E));
- case CK_PointerToBoolean:
- return EmitPointerToBoolConversion(Visit(E));
- case CK_FloatingToBoolean:
- return EmitFloatToBoolConversion(Visit(E));
- case CK_MemberPointerToBoolean: {
- llvm::Value *MemPtr = Visit(E);
- const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
- return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
- }
- case CK_FloatingComplexToReal:
- case CK_IntegralComplexToReal:
- return CGF.EmitComplexExpr(E, false, true).first;
- case CK_FloatingComplexToBoolean:
- case CK_IntegralComplexToBoolean: {
- CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
- // TODO: kill this function off, inline appropriate case here
- return EmitComplexToScalarConversion(V, E->getType(), DestTy);
- }
- }
- llvm_unreachable("unknown scalar cast");
- }
- Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
- CodeGenFunction::StmtExprEvaluation eval(CGF);
- return CGF.EmitCompoundStmt(*E->getSubStmt(), !E->getType()->isVoidType())
- .getScalarVal();
- }
- Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
- LValue LV = CGF.EmitBlockDeclRefLValue(E);
- return CGF.EmitLoadOfLValue(LV).getScalarVal();
- }
- //===----------------------------------------------------------------------===//
- // Unary Operators
- //===----------------------------------------------------------------------===//
- llvm::Value *ScalarExprEmitter::
- EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
- llvm::Value *InVal,
- llvm::Value *NextVal, bool IsInc) {
- switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) {
- case LangOptions::SOB_Undefined:
- return Builder.CreateNSWAdd(InVal, NextVal, IsInc ? "inc" : "dec");
- case LangOptions::SOB_Defined:
- return Builder.CreateAdd(InVal, NextVal, IsInc ? "inc" : "dec");
- case LangOptions::SOB_Trapping:
- BinOpInfo BinOp;
- BinOp.LHS = InVal;
- BinOp.RHS = NextVal;
- BinOp.Ty = E->getType();
- BinOp.Opcode = BO_Add;
- BinOp.E = E;
- return EmitOverflowCheckedBinOp(BinOp);
- }
- llvm_unreachable("Unknown SignedOverflowBehaviorTy");
- }
- llvm::Value *
- ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
- bool isInc, bool isPre) {
-
- QualType type = E->getSubExpr()->getType();
- llvm::Value *value = EmitLoadOfLValue(LV);
- llvm::Value *input = value;
- llvm::PHINode *atomicPHI = 0;
- int amount = (isInc ? 1 : -1);
- if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
- llvm::BasicBlock *startBB = Builder.GetInsertBlock();
- llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
- Builder.CreateBr(opBB);
- Builder.SetInsertPoint(opBB);
- atomicPHI = Builder.CreatePHI(value->getType(), 2);
- atomicPHI->addIncoming(value, startBB);
- type = atomicTy->getValueType();
- value = atomicPHI;
- }
- // Special case of integer increment that we have to check first: bool++.
- // Due to promotion rules, we get:
- // bool++ -> bool = bool + 1
- // -> bool = (int)bool + 1
- // -> bool = ((int)bool + 1 != 0)
- // An interesting aspect of this is that increment is always true.
- // Decrement does not have this property.
- if (isInc && type->isBooleanType()) {
- value = Builder.getTrue();
- // Most common case by far: integer increment.
- } else if (type->isIntegerType()) {
- llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
- // Note that signed integer inc/dec with width less than int can't
- // overflow because of promotion rules; we're just eliding a few steps here.
- if (type->isSignedIntegerOrEnumerationType() &&
- value->getType()->getPrimitiveSizeInBits() >=
- CGF.IntTy->getBitWidth())
- value = EmitAddConsiderOverflowBehavior(E, value, amt, isInc);
- else
- value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
-
- // Next most common: pointer increment.
- } else if (const PointerType *ptr = type->getAs<PointerType>()) {
- QualType type = ptr->getPointeeType();
- // VLA types don't have constant size.
- if (const VariableArrayType *vla
- = CGF.getContext().getAsVariableArrayType(type)) {
- llvm::Value *numElts = CGF.getVLASize(vla).first;
- if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize");
- if (CGF.getContext().getLangOptions().isSignedOverflowDefined())
- value = Builder.CreateGEP(value, numElts, "vla.inc");
- else
- value = Builder.CreateInBoundsGEP(value, numElts, "vla.inc");
-
- // Arithmetic on function pointers (!) is just +-1.
- } else if (type->isFunctionType()) {
- llvm::Value *amt = Builder.getInt32(amount);
- value = CGF.EmitCastToVoidPtr(value);
- if (CGF.getContext().getLangOptions().isSignedOverflowDefined())
- value = Builder.CreateGEP(value, amt, "incdec.funcptr");
- else
- value = Builder.CreateInBoundsGEP(value, amt, "incdec.funcptr");
- value = Builder.CreateBitCast(value, input->getType());
- // For everything else, we can just do a simple increment.
- } else {
- llvm::Value *amt = Builder.getInt32(amount);
- if (CGF.getContext().getLangOptions().isSignedOverflowDefined())
- value = Builder.CreateGEP(value, amt, "incdec.ptr");
- else
- value = Builder.CreateInBoundsGEP(value, amt, "incdec.ptr");
- }
- // Vector increment/decrement.
- } else if (type->isVectorType()) {
- if (type->hasIntegerRepresentation()) {
- llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
- value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
- } else {
- value = Builder.CreateFAdd(
- value,
- llvm::ConstantFP::get(value->getType(), amount),
- isInc ? "inc" : "dec");
- }
- // Floating point.
- } else if (type->isRealFloatingType()) {
- // Add the inc/dec to the real part.
- llvm::Value *amt;
- if (type->isHalfType()) {
- // Another special case: half FP increment should be done via float
- value =
- Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16),
- input);
- }
- if (value->getType()->isFloatTy())
- amt = llvm::ConstantFP::get(VMContext,
- llvm::APFloat(static_cast<float>(amount)));
- else if (value->getType()->isDoubleTy())
- amt = llvm::ConstantFP::get(VMContext,
- llvm::APFloat(static_cast<double>(amount)));
- else {
- llvm::APFloat F(static_cast<float>(amount));
- bool ignored;
- F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
- &ignored);
- amt = llvm::ConstantFP::get(VMContext, F);
- }
- value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec");
- if (type->isHalfType())
- value =
- Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16),
- value);
- // Objective-C pointer types.
- } else {
- const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>();
- value = CGF.EmitCastToVoidPtr(value);
- CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
- if (!isInc) size = -size;
- llvm::Value *sizeValue =
- llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity());
- if (CGF.getContext().getLangOptions().isSignedOverflowDefined())
- value = Builder.CreateGEP(value, sizeValue, "incdec.objptr");
- else
- value = Builder.CreateInBoundsGEP(value, sizeValue, "incdec.objptr");
- value = Builder.CreateBitCast(value, input->getType());
- }
-
- if (atomicPHI) {
- llvm::BasicBlock *opBB = Builder.GetInsertBlock();
- llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
- llvm::Value *old = Builder.CreateAtomicCmpXchg(LV.getAddress(), atomicPHI,
- value, llvm::SequentiallyConsistent);
- atomicPHI->addIncoming(old, opBB);
- llvm::Value *success = Builder.CreateICmpEQ(old, atomicPHI);
- Builder.CreateCondBr(success, contBB, opBB);
- Builder.SetInsertPoint(contBB);
- return isPre ? value : input;
- }
- // Store the updated result through the lvalue.
- if (LV.isBitField())
- CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value);
- else
- CGF.EmitStoreThroughLValue(RValue::get(value), LV);
- // If this is a postinc, return the value read from memory, otherwise use the
- // updated value.
- return isPre ? value : input;
- }
- Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
- TestAndClearIgnoreResultAssign();
- // Emit unary minus with EmitSub so we handle overflow cases etc.
- BinOpInfo BinOp;
- BinOp.RHS = Visit(E->getSubExpr());
-
- if (BinOp.RHS->getType()->isFPOrFPVectorTy())
- BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType());
- else
- BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType());
- BinOp.Ty = E->getType();
- BinOp.Opcode = BO_Sub;
- BinOp.E = E;
- return EmitSub(BinOp);
- }
- Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
- TestAndClearIgnoreResultAssign();
- Value *Op = Visit(E->getSubExpr());
- return Builder.CreateNot(Op, "neg");
- }
- Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
-
- // Perform vector logical not on comparison with zero vector.
- if (E->getType()->isExtVectorType()) {
- Value *Oper = Visit(E->getSubExpr());
- Value *Zero = llvm::Constant::getNullValue(Oper->getType());
- Value *Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp");
- return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
- }
-
- // Compare operand to zero.
- Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
- // Invert value.
- // TODO: Could dynamically modify easy computations here. For example, if
- // the operand is an icmp ne, turn into icmp eq.
- BoolVal = Builder.CreateNot(BoolVal, "lnot");
- // ZExt result to the expr type.
- return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
- }
- Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) {
- // Try folding the offsetof to a constant.
- llvm::APSInt Value;
- if (E->EvaluateAsInt(Value, CGF.getContext()))
- return Builder.getInt(Value);
- // Loop over the components of the offsetof to compute the value.
- unsigned n = E->getNumComponents();
- llvm::Type* ResultType = ConvertType(E->getType());
- llvm::Value* Result = llvm::Constant::getNullValue(ResultType);
- QualType CurrentType = E->getTypeSourceInfo()->getType();
- for (unsigned i = 0; i != n; ++i) {
- OffsetOfExpr::OffsetOfNode ON = E->getComponent(i);
- llvm::Value *Offset = 0;
- switch (ON.getKind()) {
- case OffsetOfExpr::OffsetOfNode::Array: {
- // Compute the index
- Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex());
- llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr);
- bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType();
- Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv");
- // Save the element type
- CurrentType =
- CGF.getContext().getAsArrayType(CurrentType)->getElementType();
- // Compute the element size
- llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType,
- CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity());
- // Multiply out to compute the result
- Offset = Builder.CreateMul(Idx, ElemSize);
- break;
- }
- case OffsetOfExpr::OffsetOfNode::Field: {
- FieldDecl *MemberDecl = ON.getField();
- RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
- const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
- // Compute the index of the field in its parent.
- unsigned i = 0;
- // FIXME: It would be nice if we didn't have to loop here!
- for (RecordDecl::field_iterator Field = RD->field_begin(),
- FieldEnd = RD->field_end();
- Field != FieldEnd; (void)++Field, ++i) {
- if (*Field == MemberDecl)
- break;
- }
- assert(i < RL.getFieldCount() && "offsetof field in wrong type");
- // Compute the offset to the field
- int64_t OffsetInt = RL.getFieldOffset(i) /
- CGF.getContext().getCharWidth();
- Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
- // Save the element type.
- CurrentType = MemberDecl->getType();
- break;
- }
- case OffsetOfExpr::OffsetOfNode::Identifier:
- llvm_unreachable("dependent __builtin_offsetof");
- case OffsetOfExpr::OffsetOfNode::Base: {
- if (ON.getBase()->isVirtual()) {
- CGF.ErrorUnsupported(E, "virtual base in offsetof");
- continue;
- }
- RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
- const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
- // Save the element type.
- CurrentType = ON.getBase()->getType();
-
- // Compute the offset to the base.
- const RecordType *BaseRT = CurrentType->getAs<RecordType>();
- CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl());
- int64_t OffsetInt = RL.getBaseClassOffsetInBits(BaseRD) /
- CGF.getContext().getCharWidth();
- Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
- break;
- }
- }
- Result = Builder.CreateAdd(Result, Offset);
- }
- return Result;
- }
- /// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of
- /// argument of the sizeof expression as an integer.
- Value *
- ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
- const UnaryExprOrTypeTraitExpr *E) {
- QualType TypeToSize = E->getTypeOfArgument();
- if (E->getKind() == UETT_SizeOf) {
- if (const VariableArrayType *VAT =
- CGF.getContext().getAsVariableArrayType(TypeToSize)) {
- if (E->isArgumentType()) {
- // sizeof(type) - make sure to emit the VLA size.
- CGF.EmitVariablyModifiedType(TypeToSize);
- } else {
- // C99 6.5.3.4p2: If the argument is an expression of type
- // VLA, it is evaluated.
- CGF.EmitIgnoredExpr(E->getArgumentExpr());
- }
- QualType eltType;
- llvm::Value *numElts;
- llvm::tie(numElts, eltType) = CGF.getVLASize(VAT);
- llvm::Value *size = numElts;
- // Scale the number of non-VLA elements by the non-VLA element size.
- CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
- if (!eltSize.isOne())
- size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), numElts);
- return size;
- }
- }
- // If this isn't sizeof(vla), the result must be constant; use the constant
- // folding logic so we don't have to duplicate it here.
- return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext()));
- }
- Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
- Expr *Op = E->getSubExpr();
- if (Op->getType()->isAnyComplexType()) {
- // If it's an l-value, load through the appropriate subobject l-value.
- // Note that we have to ask E because Op might be an l-value that
- // this won't work for, e.g. an Obj-C property.
- if (E->isGLValue())
- return CGF.EmitLoadOfLValue(CGF.EmitLValue(E)).getScalarVal();
- // Otherwise, calculate and project.
- return CGF.EmitComplexExpr(Op, false, true).first;
- }
- return Visit(Op);
- }
- Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
- Expr *Op = E->getSubExpr();
- if (Op->getType()->isAnyComplexType()) {
- // If it's an l-value, load through the appropriate subobject l-value.
- // Note that we have to ask E because Op might be an l-value that
- // this won't work for, e.g. an Obj-C property.
- if (Op->isGLValue())
- return CGF.EmitLoadOfLValue(CGF.EmitLValue(E)).getScalarVal();
- // Otherwise, calculate and project.
- return CGF.EmitComplexExpr(Op, true, false).second;
- }
- // __imag on a scalar returns zero. Emit the subexpr to ensure side
- // effects are evaluated, but not the actual value.
- CGF.EmitScalarExpr(Op, true);
- return llvm::Constant::getNullValue(ConvertType(E->getType()));
- }
- //===----------------------------------------------------------------------===//
- // Binary Operators
- //===----------------------------------------------------------------------===//
- BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
- TestAndClearIgnoreResultAssign();
- BinOpInfo Result;
- Result.LHS = Visit(E->getLHS());
- Result.RHS = Visit(E->getRHS());
- Result.Ty = E->getType();
- Result.Opcode = E->getOpcode();
- Result.E = E;
- return Result;
- }
- LValue ScalarExprEmitter::EmitCompoundAssignLValue(
- const CompoundAssignOperator *E,
- Value *(ScalarExprEmitter::*Func)(const BinOpInfo &),
- Value *&Result) {
- QualType LHSTy = E->getLHS()->getType();
- BinOpInfo OpInfo;
-
- if (E->getComputationResultType()->isAnyComplexType()) {
- // This needs to go through the complex expression emitter, but it's a tad
- // complicated to do that... I'm leaving it out for now. (Note that we do
- // actually need the imaginary part of the RHS for multiplication and
- // division.)
- CGF.ErrorUnsupported(E, "complex compound assignment");
- Result = llvm::UndefValue::get(CGF.ConvertType(E->getType()));
- return LValue();
- }
-
- // Emit the RHS first. __block variables need to have the rhs evaluated
- // first, plus this should improve codegen a little.
- OpInfo.RHS = Visit(E->getRHS());
- OpInfo.Ty = E->getComputationResultType();
- OpInfo.Opcode = E->getOpcode();
- OpInfo.E = E;
- // Load/convert the LHS.
- LValue LHSLV = EmitCheckedLValue(E->getLHS());
- OpInfo.LHS = EmitLoadOfLValue(LHSLV);
- OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
- E->getComputationLHSType());
- llvm::PHINode *atomicPHI = 0;
- if (const AtomicType *atomicTy = OpInfo.Ty->getAs<AtomicType>()) {
- // FIXME: For floating point types, we should be saving and restoring the
- // floating point environment in the loop.
- llvm::BasicBlock *startBB = Builder.GetInsertBlock();
- llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
- Builder.CreateBr(opBB);
- Builder.SetInsertPoint(opBB);
- atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2);
- atomicPHI->addIncoming(OpInfo.LHS, startBB);
- OpInfo.Ty = atomicTy->getValueType();
- OpInfo.LHS = atomicPHI;
- }
-
- // Expand the binary operator.
- Result = (this->*Func)(OpInfo);
-
- // Convert the result back to the LHS type.
- Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
- if (atomicPHI) {
- llvm::BasicBlock *opBB = Builder.GetInsertBlock();
- llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
- llvm::Value *old = Builder.CreateAtomicCmpXchg(LHSLV.getAddress(), atomicPHI,
- Result, llvm::SequentiallyConsistent);
- atomicPHI->addIncoming(old, opBB);
- llvm::Value *success = Builder.CreateICmpEQ(old, atomicPHI);
- Builder.CreateCondBr(success, contBB, opBB);
- Builder.SetInsertPoint(contBB);
- return LHSLV;
- }
-
- // Store the result value into the LHS lvalue. Bit-fields are handled
- // specially because the result is altered by the store, i.e., [C99 6.5.16p1]
- // 'An assignment expression has the value of the left operand after the
- // assignment...'.
- if (LHSLV.isBitField())
- CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result);
- else
- CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV);
- return LHSLV;
- }
- Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
- Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
- bool Ignore = TestAndClearIgnoreResultAssign();
- Value *RHS;
- LValue LHS = EmitCompoundAssignLValue(E, Func, RHS);
- // If the result is clearly ignored, return now.
- if (Ignore)
- return 0;
- // The result of an assignment in C is the assigned r-value.
- if (!CGF.getContext().getLangOptions().CPlusPlus)
- return RHS;
- // If the lvalue is non-volatile, return the computed value of the assignment.
- if (!LHS.isVolatileQualified())
- return RHS;
- // Otherwise, reload the value.
- return EmitLoadOfLValue(LHS);
- }
- void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
- const BinOpInfo &Ops,
- llvm::Value *Zero, bool isDiv) {
- llvm::Function::iterator insertPt = Builder.GetInsertBlock();
- llvm::BasicBlock *contBB =
- CGF.createBasicBlock(isDiv ? "div.cont" : "rem.cont", CGF.CurFn,
- llvm::next(insertPt));
- llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn);
- llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
- if (Ops.Ty->hasSignedIntegerRepresentation()) {
- llvm::Value *IntMin =
- Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth()));
- llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL);
- llvm::Value *Cond1 = Builder.CreateICmpEQ(Ops.RHS, Zero);
- llvm::Value *LHSCmp = Builder.CreateICmpEQ(Ops.LHS, IntMin);
- llvm::Value *RHSCmp = Builder.CreateICmpEQ(Ops.RHS, NegOne);
- llvm::Value *Cond2 = Builder.CreateAnd(LHSCmp, RHSCmp, "and");
- Builder.CreateCondBr(Builder.CreateOr(Cond1, Cond2, "or"),
- overflowBB, contBB);
- } else {
- CGF.Builder.CreateCondBr(Builder.CreateICmpEQ(Ops.RHS, Zero),
- overflowBB, contBB);
- }
- EmitOverflowBB(overflowBB);
- Builder.SetInsertPoint(contBB);
- }
- Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
- if (isTrapvOverflowBehavior()) {
- llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
- if (Ops.Ty->isIntegerType())
- EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true);
- else if (Ops.Ty->isRealFloatingType()) {
- llvm::Function::iterator insertPt = Builder.GetInsertBlock();
- llvm::BasicBlock *DivCont = CGF.createBasicBlock("div.cont", CGF.CurFn,
- llvm::next(insertPt));
- llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow",
- CGF.CurFn);
- CGF.Builder.CreateCondBr(Builder.CreateFCmpOEQ(Ops.RHS, Zero),
- overflowBB, DivCont);
- EmitOverflowBB(overflowBB);
- Builder.SetInsertPoint(DivCont);
- }
- }
- if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
- llvm::Value *Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
- if (CGF.getContext().getLangOptions().OpenCL) {
- // OpenCL 1.1 7.4: minimum accuracy of single precision / is 2.5ulp
- llvm::Type *ValTy = Val->getType();
- if (ValTy->isFloatTy() ||
- (isa<llvm::VectorType>(ValTy) &&
- cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy()))
- CGF.SetFPAccuracy(Val, 5, 2);
- }
- return Val;
- }
- else if (Ops.Ty->hasUnsignedIntegerRepresentation())
- return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
- else
- return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
- }
- Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
- // Rem in C can't be a floating point type: C99 6.5.5p2.
- if (isTrapvOverflowBehavior()) {
- llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
- if (Ops.Ty->isIntegerType())
- EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false);
- }
- if (Ops.Ty->hasUnsignedIntegerRepresentation())
- return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
- else
- return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
- }
- Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
- unsigned IID;
- unsigned OpID = 0;
- switch (Ops.Opcode) {
- case BO_Add:
- case BO_AddAssign:
- OpID = 1;
- IID = llvm::Intrinsic::sadd_with_overflow;
- break;
- case BO_Sub:
- case BO_SubAssign:
- OpID = 2;
- IID = llvm::Intrinsic::ssub_with_overflow;
- break;
- case BO_Mul:
- case BO_MulAssign:
- OpID = 3;
- IID = llvm::Intrinsic::smul_with_overflow;
- break;
- default:
- llvm_unreachable("Unsupported operation for overflow detection");
- }
- OpID <<= 1;
- OpID |= 1;
- llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
- llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy);
- Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
- Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
- Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
- // Branch in case of overflow.
- llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
- llvm::Function::iterator insertPt = initialBB;
- llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn,
- llvm::next(insertPt));
- llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn);
- Builder.CreateCondBr(overflow, overflowBB, continueBB);
- // Handle overflow with llvm.trap.
- const std::string *handlerName =
- &CGF.getContext().getLangOptions().OverflowHandler;
- if (handlerName->empty()) {
- EmitOverflowBB(overflowBB);
- Builder.SetInsertPoint(continueBB);
- return result;
- }
- // If an overflow handler is set, then we want to call it and then use its
- // result, if it returns.
- Builder.SetInsertPoint(overflowBB);
- // Get the overflow handler.
- llvm::Type *Int8Ty = CGF.Int8Ty;
- llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
- llvm::FunctionType *handlerTy =
- llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
- llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
- // Sign extend the args to 64-bit, so that we can use the same handler for
- // all types of overflow.
- llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty);
- llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty);
- // Call the handler with the two arguments, the operation, and the size of
- // the result.
- llvm::Value *handlerResult = Builder.CreateCall4(handler, lhs, rhs,
- Builder.getInt8(OpID),
- Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth()));
- // Truncate the result back to the desired size.
- handlerResult = Builder.CreateTrunc(handlerResult, opTy);
- Builder.CreateBr(continueBB);
- Builder.SetInsertPoint(continueBB);
- llvm::PHINode *phi = Builder.CreatePHI(opTy, 2);
- phi->addIncoming(result, initialBB);
- phi->addIncoming(handlerResult, overflowBB);
- return phi;
- }
- /// Emit pointer + index arithmetic.
- static Value *emitPointerArithmetic(CodeGenFunction &CGF,
- const BinOpInfo &op,
- bool isSubtraction) {
- // Must have binary (not unary) expr here. Unary pointer
- // increment/decrement doesn't use this path.
- const BinaryOperator *expr = cast<BinaryOperator>(op.E);
-
- Value *pointer = op.LHS;
- Expr *pointerOperand = expr->getLHS();
- Value *index = op.RHS;
- Expr *indexOperand = expr->getRHS();
- // In a subtraction, the LHS is always the pointer.
- if (!isSubtraction && !pointer->getType()->isPointerTy()) {
- std::swap(pointer, index);
- std::swap(pointerOperand, indexOperand);
- }
- unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth();
- if (width != CGF.PointerWidthInBits) {
- // Zero-extend or sign-extend the pointer value according to
- // whether the index is signed or not.
- bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType();
- index = CGF.Builder.CreateIntCast(index, CGF.PtrDiffTy, isSigned,
- "idx.ext");
- }
- // If this is subtraction, negate the index.
- if (isSubtraction)
- index = CGF.Builder.CreateNeg(index, "idx.neg");
- const PointerType *pointerType
- = pointerOperand->getType()->getAs<PointerType>();
- if (!pointerType) {
- QualType objectType = pointerOperand->getType()
- ->castAs<ObjCObjectPointerType>()
- ->getPointeeType();
- llvm::Value *objectSize
- = CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType));
- index = CGF.Builder.CreateMul(index, objectSize);
- Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
- result = CGF.Builder.CreateGEP(result, index, "add.ptr");
- return CGF.Builder.CreateBitCast(result, pointer->getType());
- }
- QualType elementType = pointerType->getPointeeType();
- if (const VariableArrayType *vla
- = CGF.getContext().getAsVariableArrayType(elementType)) {
- // The element count here is the total number of non-VLA elements.
- llvm::Value *numElements = CGF.getVLASize(vla).first;
- // Effectively, the multiply by the VLA size is part of the GEP.
- // GEP indexes are signed, and scaling an index isn't permitted to
- // signed-overflow, so we use the same semantics for our explicit
- // multiply. We suppress this if overflow is not undefined behavior.
- if (CGF.getLangOptions().isSignedOverflowDefined()) {
- index = CGF.Builder.CreateMul(index, numElements, "vla.index");
- pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr");
- } else {
- index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index");
- pointer = CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
- }
- return pointer;
- }
- // Explicitly handle GNU void* and function pointer arithmetic extensions. The
- // GNU void* casts amount to no-ops since our void* type is i8*, but this is
- // future proof.
- if (elementType->isVoidType() || elementType->isFunctionType()) {
- Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
- result = CGF.Builder.CreateGEP(result, index, "add.ptr");
- return CGF.Builder.CreateBitCast(result, pointer->getType());
- }
- if (CGF.getLangOptions().isSignedOverflowDefined())
- return CGF.Builder.CreateGEP(pointer, index, "add.ptr");
- return CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
- }
- Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) {
- if (op.LHS->getType()->isPointerTy() ||
- op.RHS->getType()->isPointerTy())
- return emitPointerArithmetic(CGF, op, /*subtraction*/ false);
- if (op.Ty->isSignedIntegerOrEnumerationType()) {
- switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) {
- case LangOptions::SOB_Undefined:
- return Builder.CreateNSWAdd(op.LHS, op.RHS, "add");
- case LangOptions::SOB_Defined:
- return Builder.CreateAdd(op.LHS, op.RHS, "add");
- case LangOptions::SOB_Trapping:
- return EmitOverflowCheckedBinOp(op);
- }
- }
-
- if (op.LHS->getType()->isFPOrFPVectorTy())
- return Builder.CreateFAdd(op.LHS, op.RHS, "add");
- return Builder.CreateAdd(op.LHS, op.RHS, "add");
- }
- Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
- // The LHS is always a pointer if either side is.
- if (!op.LHS->getType()->isPointerTy()) {
- if (op.Ty->isSignedIntegerOrEnumerationType()) {
- switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) {
- case LangOptions::SOB_Undefined:
- return Builder.CreateNSWSub(op.LHS, op.RHS, "sub");
- case LangOptions::SOB_Defined:
- return Builder.CreateSub(op.LHS, op.RHS, "sub");
- case LangOptions::SOB_Trapping:
- return EmitOverflowCheckedBinOp(op);
- }
- }
-
- if (op.LHS->getType()->isFPOrFPVectorTy())
- return Builder.CreateFSub(op.LHS, op.RHS, "sub");
- return Builder.CreateSub(op.LHS, op.RHS, "sub");
- }
- // If the RHS is not a pointer, then we have normal pointer
- // arithmetic.
- if (!op.RHS->getType()->isPointerTy())
- return emitPointerArithmetic(CGF, op, /*subtraction*/ true);
- // Otherwise, this is a pointer subtraction.
- // Do the raw subtraction part.
- llvm::Value *LHS
- = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast");
- llvm::Value *RHS
- = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast");
- Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
- // Okay, figure out the element size.
- const BinaryOperator *expr = cast<BinaryOperator>(op.E);
- QualType elementType = expr->getLHS()->getType()->getPointeeType();
- llvm::Value *divisor = 0;
- // For a variable-length array, this is going to be non-constant.
- if (const VariableArrayType *vla
- = CGF.getContext().getAsVariableArrayType(elementType)) {
- llvm::Value *numElements;
- llvm::tie(numElements, elementType) = CGF.getVLASize(vla);
- divisor = numElements;
- // Scale the number of non-VLA elements by the non-VLA element size.
- CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType);
- if (!eltSize.isOne())
- divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor);
- // For everything elese, we can just compute it, safe in the
- // assumption that Sema won't let anything through that we can't
- // safely compute the size of.
- } else {
- CharUnits elementSize;
- // Handle GCC extension for pointer arithmetic on void* and
- // function pointer types.
- if (elementType->isVoidType() || elementType->isFunctionType())
- elementSize = CharUnits::One();
- else
- elementSize = CGF.getContext().getTypeSizeInChars(elementType);
- // Don't even emit the divide for element size of 1.
- if (elementSize.isOne())
- return diffInChars;
- divisor = CGF.CGM.getSize(elementSize);
- }
-
- // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
- // pointer difference in C is only defined in the case where both operands
- // are pointing to elements of an array.
- return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div");
- }
- Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
- // LLVM requires the LHS and RHS to be the same type: promote or truncate the
- // RHS to the same size as the LHS.
- Value *RHS = Ops.RHS;
- if (Ops.LHS->getType() != RHS->getType())
- RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
- if (CGF.CatchUndefined
- && isa<llvm::IntegerType>(Ops.LHS->getType())) {
- unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
- llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
- CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
- llvm::ConstantInt::get(RHS->getType(), Width)),
- Cont, CGF.getTrapBB());
- CGF.EmitBlock(Cont);
- }
- return Builder.CreateShl(Ops.LHS, RHS, "shl");
- }
- Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
- // LLVM requires the LHS and RHS to be the same type: promote or truncate the
- // RHS to the same size as the LHS.
- Value *RHS = Ops.RHS;
- if (Ops.LHS->getType() != RHS->getType())
- RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
- if (CGF.CatchUndefined
- && isa<llvm::IntegerType>(Ops.LHS->getType())) {
- unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
- llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
- CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
- llvm::ConstantInt::get(RHS->getType(), Width)),
- Cont, CGF.getTrapBB());
- CGF.EmitBlock(Cont);
- }
- if (Ops.Ty->hasUnsignedIntegerRepresentation())
- return Builder.CreateLShr(Ops.LHS, RHS, "shr");
- return Builder.CreateAShr(Ops.LHS, RHS, "shr");
- }
- enum IntrinsicType { VCMPEQ, VCMPGT };
- // return corresponding comparison intrinsic for given vector type
- static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT,
- BuiltinType::Kind ElemKind) {
- switch (ElemKind) {
- default: llvm_unreachable("unexpected element type");
- case BuiltinType::Char_U:
- case BuiltinType::UChar:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
- case BuiltinType::Char_S:
- case BuiltinType::SChar:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
- case BuiltinType::UShort:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
- case BuiltinType::Short:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
- case BuiltinType::UInt:
- case BuiltinType::ULong:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
- case BuiltinType::Int:
- case BuiltinType::Long:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
- case BuiltinType::Float:
- return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
- llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
- }
- }
- Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
- unsigned SICmpOpc, unsigned FCmpOpc) {
- TestAndClearIgnoreResultAssign();
- Value *Result;
- QualType LHSTy = E->getLHS()->getType();
- if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) {
- assert(E->getOpcode() == BO_EQ ||
- E->getOpcode() == BO_NE);
- Value *LHS = CGF.EmitScalarExpr(E->getLHS());
- Value *RHS = CGF.EmitScalarExpr(E->getRHS());
- Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison(
- CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE);
- } else if (!LHSTy->isAnyComplexType()) {
- Value *LHS = Visit(E->getLHS());
- Value *RHS = Visit(E->getRHS());
- // If AltiVec, the comparison results in a numeric type, so we use
- // intrinsics comparing vectors and giving 0 or 1 as a result
- if (LHSTy->isVectorType() && !E->getType()->isVectorType()) {
- // constants for mapping CR6 register bits to predicate result
- enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
- llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic;
- // in several cases vector arguments order will be reversed
- Value *FirstVecArg = LHS,
- *SecondVecArg = RHS;
- QualType ElTy = LHSTy->getAs<VectorType>()->getElementType();
- const BuiltinType *BTy = ElTy->getAs<BuiltinType>();
- BuiltinType::Kind ElementKind = BTy->getKind();
- switch(E->getOpcode()) {
- default: llvm_unreachable("is not a comparison operation");
- case BO_EQ:
- CR6 = CR6_LT;
- ID = GetIntrinsic(VCMPEQ, ElementKind);
- break;
- case BO_NE:
- CR6 = CR6_EQ;
- ID = GetIntrinsic(VCMPEQ, ElementKind);
- break;
- case BO_LT:
- CR6 = CR6_LT;
- ID = GetIntrinsic(VCMPGT, ElementKind);
- std::swap(FirstVecArg, SecondVecArg);
- break;
- case BO_GT:
- CR6 = CR6_LT;
- ID = GetIntrinsic(VCMPGT, ElementKind);
- break;
- case BO_LE:
- if (ElementKind == BuiltinType::Float) {
- CR6 = CR6_LT;
- ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
- std::swap(FirstVecArg, SecondVecArg);
- }
- else {
- CR6 = CR6_EQ;
- ID = GetIntrinsic(VCMPGT, ElementKind);
- }
- break;
- case BO_GE:
- if (ElementKind == BuiltinType::Float) {
- CR6 = CR6_LT;
- ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
- }
- else {
- CR6 = CR6_EQ;
- ID = GetIntrinsic(VCMPGT, ElementKind);
- std::swap(FirstVecArg, SecondVecArg);
- }
- break;
- }
- Value *CR6Param = Builder.getInt32(CR6);
- llvm::Function *F = CGF.CGM.getIntrinsic(ID);
- Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, "");
- return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
- }
- if (LHS->getType()->isFPOrFPVectorTy()) {
- Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
- LHS, RHS, "cmp");
- } else if (LHSTy->hasSignedIntegerRepresentation()) {
- Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
- LHS, RHS, "cmp");
- } else {
- // Unsigned integers and pointers.
- Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
- LHS, RHS, "cmp");
- }
- // If this is a vector comparison, sign extend the result to the appropriate
- // vector integer type and return it (don't convert to bool).
- if (LHSTy->isVectorType())
- return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
- } else {
- // Complex Comparison: can only be an equality comparison.
- CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
- CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
- QualType CETy = LHSTy->getAs<ComplexType>()->getElementType();
- Value *ResultR, *ResultI;
- if (CETy->isRealFloatingType()) {
- ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
- LHS.first, RHS.first, "cmp.r");
- ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
- LHS.second, RHS.second, "cmp.i");
- } else {
- // Complex comparisons can only be equality comparisons. As such, signed
- // and unsigned opcodes are the same.
- ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
- LHS.first, RHS.first, "cmp.r");
- ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
- LHS.second, RHS.second, "cmp.i");
- }
- if (E->getOpcode() == BO_EQ) {
- Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
- } else {
- assert(E->getOpcode() == BO_NE &&
- "Complex comparison other than == or != ?");
- Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
- }
- }
- return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
- }
- Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
- bool Ignore = TestAndClearIgnoreResultAssign();
- Value *RHS;
- LValue LHS;
- switch (E->getLHS()->getType().getObjCLifetime()) {
- case Qualifiers::OCL_Strong:
- llvm::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore);
- break;
- case Qualifiers::OCL_Autoreleasing:
- llvm::tie(LHS,RHS) = CGF.EmitARCStoreAutoreleasing(E);
- break;
- case Qualifiers::OCL_Weak:
- RHS = Visit(E->getRHS());
- LHS = EmitCheckedLValue(E->getLHS());
- RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore);
- break;
- // No reason to do any of these differently.
- case Qualifiers::OCL_None:
- case Qualifiers::OCL_ExplicitNone:
- // __block variables need to have the rhs evaluated first, plus
- // this should improve codegen just a little.
- RHS = Visit(E->getRHS());
- LHS = EmitCheckedLValue(E->getLHS());
- // Store the value into the LHS. Bit-fields are handled specially
- // because the result is altered by the store, i.e., [C99 6.5.16p1]
- // 'An assignment expression has the value of the left operand after
- // the assignment...'.
- if (LHS.isBitField())
- CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS);
- else
- CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);
- }
- // If the result is clearly ignored, return now.
- if (Ignore)
- return 0;
- // The result of an assignment in C is the assigned r-value.
- if (!CGF.getContext().getLangOptions().CPlusPlus)
- return RHS;
- // If the lvalue is non-volatile, return the computed value of the assignment.
- if (!LHS.isVolatileQualified())
- return RHS;
- // Otherwise, reload the value.
- return EmitLoadOfLValue(LHS);
- }
- Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
-
- // Perform vector logical and on comparisons with zero vectors.
- if (E->getType()->isVectorType()) {
- Value *LHS = Visit(E->getLHS());
- Value *RHS = Visit(E->getRHS());
- Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
- LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
- RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
- Value *And = Builder.CreateAnd(LHS, RHS);
- return Builder.CreateSExt(And, Zero->getType(), "sext");
- }
-
- llvm::Type *ResTy = ConvertType(E->getType());
-
- // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
- // If we have 1 && X, just emit X without inserting the control flow.
- bool LHSCondVal;
- if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
- if (LHSCondVal) { // If we have 1 && X, just emit X.
- Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
- // ZExt result to int or bool.
- return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
- }
- // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
- if (!CGF.ContainsLabel(E->getRHS()))
- return llvm::Constant::getNullValue(ResTy);
- }
- llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
- llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
- CodeGenFunction::ConditionalEvaluation eval(CGF);
- // Branch on the LHS first. If it is false, go to the failure (cont) block.
- CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
- // Any edges into the ContBlock are now from an (indeterminate number of)
- // edges from this first condition. All of these values will be false. Start
- // setting up the PHI node in the Cont Block for this.
- llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
- "", ContBlock);
- for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
- PI != PE; ++PI)
- PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
- eval.begin(CGF);
- CGF.EmitBlock(RHSBlock);
- Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
- eval.end(CGF);
- // Reaquire the RHS block, as there may be subblocks inserted.
- RHSBlock = Builder.GetInsertBlock();
- // Emit an unconditional branch from this block to ContBlock. Insert an entry
- // into the phi node for the edge with the value of RHSCond.
- if (CGF.getDebugInfo())
- // There is no need to emit line number for unconditional branch.
- Builder.SetCurrentDebugLocation(llvm::DebugLoc());
- CGF.EmitBlock(ContBlock);
- PN->addIncoming(RHSCond, RHSBlock);
- // ZExt result to int.
- return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
- }
- Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
-
- // Perform vector logical or on comparisons with zero vectors.
- if (E->getType()->isVectorType()) {
- Value *LHS = Visit(E->getLHS());
- Value *RHS = Visit(E->getRHS());
- Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
- LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
- RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
- Value *Or = Builder.CreateOr(LHS, RHS);
- return Builder.CreateSExt(Or, Zero->getType(), "sext");
- }
-
- llvm::Type *ResTy = ConvertType(E->getType());
-
- // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
- // If we have 0 || X, just emit X without inserting the control flow.
- bool LHSCondVal;
- if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
- if (!LHSCondVal) { // If we have 0 || X, just emit X.
- Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
- // ZExt result to int or bool.
- return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
- }
- // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
- if (!CGF.ContainsLabel(E->getRHS()))
- return llvm::ConstantInt::get(ResTy, 1);
- }
- llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
- llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
- CodeGenFunction::ConditionalEvaluation eval(CGF);
- // Branch on the LHS first. If it is true, go to the success (cont) block.
- CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
- // Any edges into the ContBlock are now from an (indeterminate number of)
- // edges from this first condition. All of these values will be true. Start
- // setting up the PHI node in the Cont Block for this.
- llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
- "", ContBlock);
- for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
- PI != PE; ++PI)
- PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
- eval.begin(CGF);
- // Emit the RHS condition as a bool value.
- CGF.EmitBlock(RHSBlock);
- Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
- eval.end(CGF);
- // Reaquire the RHS block, as there may be subblocks inserted.
- RHSBlock = Builder.GetInsertBlock();
- // Emit an unconditional branch from this block to ContBlock. Insert an entry
- // into the phi node for the edge with the value of RHSCond.
- CGF.EmitBlock(ContBlock);
- PN->addIncoming(RHSCond, RHSBlock);
- // ZExt result to int.
- return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
- }
- Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
- CGF.EmitIgnoredExpr(E->getLHS());
- CGF.EnsureInsertPoint();
- return Visit(E->getRHS());
- }
- //===----------------------------------------------------------------------===//
- // Other Operators
- //===----------------------------------------------------------------------===//
- /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
- /// expression is cheap enough and side-effect-free enough to evaluate
- /// unconditionally instead of conditionally. This is used to convert control
- /// flow into selects in some cases.
- static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
- CodeGenFunction &CGF) {
- E = E->IgnoreParens();
- // Anything that is an integer or floating point constant is fine.
- if (E->isConstantInitializer(CGF.getContext(), false))
- return true;
- // Non-volatile automatic variables too, to get "cond ? X : Y" where
- // X and Y are local variables.
- if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
- if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
- if (VD->hasLocalStorage() && !(CGF.getContext()
- .getCanonicalType(VD->getType())
- .isVolatileQualified()))
- return true;
- return false;
- }
- Value *ScalarExprEmitter::
- VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
- TestAndClearIgnoreResultAssign();
- // Bind the common expression if necessary.
- CodeGenFunction::OpaqueValueMapping binding(CGF, E);
- Expr *condExpr = E->getCond();
- Expr *lhsExpr = E->getTrueExpr();
- Expr *rhsExpr = E->getFalseExpr();
- // If the condition constant folds and can be elided, try to avoid emitting
- // the condition and the dead arm.
- bool CondExprBool;
- if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
- Expr *live = lhsExpr, *dead = rhsExpr;
- if (!CondExprBool) std::swap(live, dead);
- // If the dead side doesn't have labels we need, just emit the Live part.
- if (!CGF.ContainsLabel(dead)) {
- Value *Result = Visit(live);
- // If the live part is a throw expression, it acts like it has a void
- // type, so evaluating it returns a null Value*. However, a conditional
- // with non-void type must return a non-null Value*.
- if (!Result && !E->getType()->isVoidType())
- Result = llvm::UndefValue::get(CGF.ConvertType(E->getType()));
- return Result;
- }
- }
- // OpenCL: If the condition is a vector, we can treat this condition like
- // the select function.
- if (CGF.getContext().getLangOptions().OpenCL
- && condExpr->getType()->isVectorType()) {
- llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
- llvm::Value *LHS = Visit(lhsExpr);
- llvm::Value *RHS = Visit(rhsExpr);
-
- llvm::Type *condType = ConvertType(condExpr->getType());
- llvm::VectorType *vecTy = cast<llvm::VectorType>(condType);
-
- unsigned numElem = vecTy->getNumElements();
- llvm::Type *elemType = vecTy->getElementType();
-
- llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy);
- llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec);
- llvm::Value *tmp = Builder.CreateSExt(TestMSB,
- llvm::VectorType::get(elemType,
- numElem),
- "sext");
- llvm::Value *tmp2 = Builder.CreateNot(tmp);
-
- // Cast float to int to perform ANDs if necessary.
- llvm::Value *RHSTmp = RHS;
- llvm::Value *LHSTmp = LHS;
- bool wasCast = false;
- llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType());
- if (rhsVTy->getElementType()->isFloatTy()) {
- RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType());
- LHSTmp = Builder.CreateBitCast(LHS, tmp->getType());
- wasCast = true;
- }
-
- llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2);
- llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp);
- llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond");
- if (wasCast)
- tmp5 = Builder.CreateBitCast(tmp5, RHS->getType());
- return tmp5;
- }
-
- // If this is a really simple expression (like x ? 4 : 5), emit this as a
- // select instead of as control flow. We can only do this if it is cheap and
- // safe to evaluate the LHS and RHS unconditionally.
- if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) &&
- isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) {
- llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
- llvm::Value *LHS = Visit(lhsExpr);
- llvm::Value *RHS = Visit(rhsExpr);
- if (!LHS) {
- // If the conditional has void type, make sure we return a null Value*.
- assert(!RHS && "LHS and RHS types must match");
- return 0;
- }
- return Builder.CreateSelect(CondV, LHS, RHS, "cond");
- }
- llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
- llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
- llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
- CodeGenFunction::ConditionalEvaluation eval(CGF);
- CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock);
- CGF.EmitBlock(LHSBlock);
- eval.begin(CGF);
- Value *LHS = Visit(lhsExpr);
- eval.end(CGF);
- LHSBlock = Builder.GetInsertBlock();
- Builder.CreateBr(ContBlock);
- CGF.EmitBlock(RHSBlock);
- eval.begin(CGF);
- Value *RHS = Visit(rhsExpr);
- eval.end(CGF);
- RHSBlock = Builder.GetInsertBlock();
- CGF.EmitBlock(ContBlock);
- // If the LHS or RHS is a throw expression, it will be legitimately null.
- if (!LHS)
- return RHS;
- if (!RHS)
- return LHS;
- // Create a PHI node for the real part.
- llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond");
- PN->addIncoming(LHS, LHSBlock);
- PN->addIncoming(RHS, RHSBlock);
- return PN;
- }
- Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
- return Visit(E->getChosenSubExpr(CGF.getContext()));
- }
- Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
- llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
- llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
- // If EmitVAArg fails, we fall back to the LLVM instruction.
- if (!ArgPtr)
- return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
- // FIXME Volatility.
- return Builder.CreateLoad(ArgPtr);
- }
- Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) {
- return CGF.EmitBlockLiteral(block);
- }
- Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) {
- Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
- llvm::Type *DstTy = ConvertType(E->getType());
-
- // Going from vec4->vec3 or vec3->vec4 is a special case and requires
- // a shuffle vector instead of a bitcast.
- llvm::Type *SrcTy = Src->getType();
- if (isa<llvm::VectorType>(DstTy) && isa<llvm::VectorType>(SrcTy)) {
- unsigned numElementsDst = cast<llvm::VectorType>(DstTy)->getNumElements();
- unsigned numElementsSrc = cast<llvm::VectorType>(SrcTy)->getNumElements();
- if ((numElementsDst == 3 && numElementsSrc == 4)
- || (numElementsDst == 4 && numElementsSrc == 3)) {
-
-
- // In the case of going from int4->float3, a bitcast is needed before
- // doing a shuffle.
- llvm::Type *srcElemTy =
- cast<llvm::VectorType>(SrcTy)->getElementType();
- llvm::Type *dstElemTy =
- cast<llvm::VectorType>(DstTy)->getElementType();
-
- if ((srcElemTy->isIntegerTy() && dstElemTy->isFloatTy())
- || (srcElemTy->isFloatTy() && dstElemTy->isIntegerTy())) {
- // Create a float type of the same size as the source or destination.
- llvm::VectorType *newSrcTy = llvm::VectorType::get(dstElemTy,
- numElementsSrc);
-
- Src = Builder.CreateBitCast(Src, newSrcTy, "astypeCast");
- }
-
- llvm::Value *UnV = llvm::UndefValue::get(Src->getType());
-
- SmallVector<llvm::Constant*, 3> Args;
- Args.push_back(Builder.getInt32(0));
- Args.push_back(Builder.getInt32(1));
- Args.push_back(Builder.getInt32(2));
-
- if (numElementsDst == 4)
- Args.push_back(llvm::UndefValue::get(CGF.Int32Ty));
-
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
-
- return Builder.CreateShuffleVector(Src, UnV, Mask, "astype");
- }
- }
-
- return Builder.CreateBitCast(Src, DstTy, "astype");
- }
- Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
- return CGF.EmitAtomicExpr(E).getScalarVal();
- }
- //===----------------------------------------------------------------------===//
- // Entry Point into this File
- //===----------------------------------------------------------------------===//
- /// EmitScalarExpr - Emit the computation of the specified expression of scalar
- /// type, ignoring the result.
- Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
- assert(E && !hasAggregateLLVMType(E->getType()) &&
- "Invalid scalar expression to emit");
- if (isa<CXXDefaultArgExpr>(E))
- disableDebugInfo();
- Value *V = ScalarExprEmitter(*this, IgnoreResultAssign)
- .Visit(const_cast<Expr*>(E));
- if (isa<CXXDefaultArgExpr>(E))
- enableDebugInfo();
- return V;
- }
- /// EmitScalarConversion - Emit a conversion from the specified type to the
- /// specified destination type, both of which are LLVM scalar types.
- Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
- QualType DstTy) {
- assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
- "Invalid scalar expression to emit");
- return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
- }
- /// EmitComplexToScalarConversion - Emit a conversion from the specified complex
- /// type to the specified destination type, where the destination type is an
- /// LLVM scalar type.
- Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
- QualType SrcTy,
- QualType DstTy) {
- assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
- "Invalid complex -> scalar conversion");
- return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
- DstTy);
- }
- llvm::Value *CodeGenFunction::
- EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
- bool isInc, bool isPre) {
- return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre);
- }
- LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) {
- llvm::Value *V;
- // object->isa or (*object).isa
- // Generate code as for: *(Class*)object
- // build Class* type
- llvm::Type *ClassPtrTy = ConvertType(E->getType());
- Expr *BaseExpr = E->getBase();
- if (BaseExpr->isRValue()) {
- V = CreateMemTemp(E->getType(), "resval");
- llvm::Value *Src = EmitScalarExpr(BaseExpr);
- Builder.CreateStore(Src, V);
- V = ScalarExprEmitter(*this).EmitLoadOfLValue(
- MakeNaturalAlignAddrLValue(V, E->getType()));
- } else {
- if (E->isArrow())
- V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr);
- else
- V = EmitLValue(BaseExpr).getAddress();
- }
-
- // build Class* type
- ClassPtrTy = ClassPtrTy->getPointerTo();
- V = Builder.CreateBitCast(V, ClassPtrTy);
- return MakeNaturalAlignAddrLValue(V, E->getType());
- }
- LValue CodeGenFunction::EmitCompoundAssignmentLValue(
- const CompoundAssignOperator *E) {
- ScalarExprEmitter Scalar(*this);
- Value *Result = 0;
- switch (E->getOpcode()) {
- #define COMPOUND_OP(Op) \
- case BO_##Op##Assign: \
- return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
- Result)
- COMPOUND_OP(Mul);
- COMPOUND_OP(Div);
- COMPOUND_OP(Rem);
- COMPOUND_OP(Add);
- COMPOUND_OP(Sub);
- COMPOUND_OP(Shl);
- COMPOUND_OP(Shr);
- COMPOUND_OP(And);
- COMPOUND_OP(Xor);
- COMPOUND_OP(Or);
- #undef COMPOUND_OP
-
- case BO_PtrMemD:
- case BO_PtrMemI:
- case BO_Mul:
- case BO_Div:
- case BO_Rem:
- case BO_Add:
- case BO_Sub:
- case BO_Shl:
- case BO_Shr:
- case BO_LT:
- case BO_GT:
- case BO_LE:
- case BO_GE:
- case BO_EQ:
- case BO_NE:
- case BO_And:
- case BO_Xor:
- case BO_Or:
- case BO_LAnd:
- case BO_LOr:
- case BO_Assign:
- case BO_Comma:
- llvm_unreachable("Not valid compound assignment operators");
- }
-
- llvm_unreachable("Unhandled compound assignment operator");
- }
|