2007-06-02 02:02:12 +08:00
|
|
|
//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-02 02:02:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Stmt nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-08 16:54:20 +08:00
|
|
|
#include "CGDebugInfo.h"
|
|
|
|
#include "CodeGenModule.h"
|
2007-06-02 02:02:12 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2008-02-06 00:35:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-12-12 13:52:00 +08:00
|
|
|
#include "llvm/InlineAsm.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2009-01-12 10:22:13 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-02 02:02:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statement Emission
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
void CodeGenFunction::EmitStopPoint(const Stmt *S) {
|
|
|
|
if (CGDebugInfo *DI = CGM.getDebugInfo()) {
|
|
|
|
DI->setLocation(S->getLocStart());
|
|
|
|
DI->EmitStopPoint(CurFn, Builder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
void CodeGenFunction::EmitStmt(const Stmt *S) {
|
|
|
|
assert(S && "Null statement?");
|
2008-11-12 07:11:34 +08:00
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// Check if we can handle this without bothering to generate an
|
|
|
|
// insert point or debug info.
|
|
|
|
if (EmitSimpleStmt(S))
|
|
|
|
return;
|
|
|
|
|
2008-11-12 07:11:34 +08:00
|
|
|
// If we happen to be at an unreachable point just create a dummy
|
|
|
|
// basic block to hold the code. We could change parts of irgen to
|
|
|
|
// simply not generate this code, but this situation is rare and
|
|
|
|
// probably not worth the effort.
|
|
|
|
// FIXME: Verify previous performance/effort claim.
|
|
|
|
EnsureInsertPoint();
|
2007-06-02 02:02:12 +08:00
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// Generate a stoppoint if we are emitting debug info.
|
|
|
|
EmitStopPoint(S);
|
2008-05-08 16:54:20 +08:00
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
2007-08-27 06:58:05 +08:00
|
|
|
// Must be an expression in a stmt context. Emit the value (to get
|
|
|
|
// side-effects) and ignore the result.
|
2007-06-02 02:02:12 +08:00
|
|
|
if (const Expr *E = dyn_cast<Expr>(S)) {
|
2007-08-27 06:58:05 +08:00
|
|
|
if (!hasAggregateLLVMType(E->getType()))
|
|
|
|
EmitScalarExpr(E);
|
2008-04-05 00:54:41 +08:00
|
|
|
else if (E->getType()->isAnyComplexType())
|
2007-08-27 06:58:05 +08:00
|
|
|
EmitComplexExpr(E);
|
|
|
|
else
|
|
|
|
EmitAggExpr(E, 0, false);
|
2007-06-02 02:02:12 +08:00
|
|
|
} else {
|
2008-08-16 08:56:44 +08:00
|
|
|
ErrorUnsupported(S, "statement");
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2008-08-05 00:51:22 +08:00
|
|
|
case Stmt::IndirectGotoStmtClass:
|
|
|
|
EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
|
2007-06-05 11:59:43 +08:00
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
|
2007-06-05 11:59:43 +08:00
|
|
|
case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
|
2007-06-06 04:53:16 +08:00
|
|
|
case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
|
|
|
|
case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
|
2007-06-05 11:59:43 +08:00
|
|
|
|
2007-06-02 11:19:07 +08:00
|
|
|
case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
|
2007-06-02 12:16:21 +08:00
|
|
|
case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
|
2008-10-03 02:02:06 +08:00
|
|
|
|
2007-10-05 07:45:31 +08:00
|
|
|
case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
|
2008-02-06 00:35:33 +08:00
|
|
|
case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
|
2008-08-23 18:51:21 +08:00
|
|
|
|
|
|
|
case Stmt::ObjCAtTryStmtClass:
|
2008-09-09 18:04:29 +08:00
|
|
|
EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
|
|
|
|
break;
|
2008-08-23 18:51:21 +08:00
|
|
|
case Stmt::ObjCAtCatchStmtClass:
|
2008-09-11 17:15:33 +08:00
|
|
|
assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
|
|
|
|
break;
|
2008-08-23 18:51:21 +08:00
|
|
|
case Stmt::ObjCAtFinallyStmtClass:
|
2008-09-09 18:04:29 +08:00
|
|
|
assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
|
2008-08-23 18:51:21 +08:00
|
|
|
break;
|
|
|
|
case Stmt::ObjCAtThrowStmtClass:
|
2008-09-09 18:04:29 +08:00
|
|
|
EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
|
2008-08-23 18:51:21 +08:00
|
|
|
break;
|
|
|
|
case Stmt::ObjCAtSynchronizedStmtClass:
|
2008-11-16 05:26:17 +08:00
|
|
|
EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
|
2008-08-23 18:51:21 +08:00
|
|
|
break;
|
2008-08-31 03:51:14 +08:00
|
|
|
case Stmt::ObjCForCollectionStmtClass:
|
|
|
|
EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
|
2008-08-23 18:51:21 +08:00
|
|
|
break;
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Stmt::NullStmtClass: break;
|
|
|
|
case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
|
|
|
|
case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
|
|
|
|
case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
|
|
|
|
case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break;
|
|
|
|
case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
|
|
|
|
case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
|
|
|
|
case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-01 06:09:40 +08:00
|
|
|
/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
|
|
|
|
/// this captures the expression result of the last sub-statement and returns it
|
|
|
|
/// (for use by the statement expression extension).
|
2007-09-01 06:49:20 +08:00
|
|
|
RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
|
|
|
|
llvm::Value *AggLoc, bool isAggVol) {
|
2007-06-02 02:02:12 +08:00
|
|
|
// FIXME: handle vla's etc.
|
2008-05-25 13:15:42 +08:00
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if (DI) {
|
2008-11-12 16:21:33 +08:00
|
|
|
EnsureInsertPoint();
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(S.getLBracLoc());
|
2008-05-25 13:15:42 +08:00
|
|
|
DI->EmitRegionStart(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2008-12-12 13:52:00 +08:00
|
|
|
// Push a null stack save value.
|
|
|
|
StackSaveValues.push_back(0);
|
|
|
|
|
2007-09-01 06:09:40 +08:00
|
|
|
for (CompoundStmt::const_body_iterator I = S.body_begin(),
|
|
|
|
E = S.body_end()-GetLast; I != E; ++I)
|
2007-06-02 02:02:12 +08:00
|
|
|
EmitStmt(*I);
|
2008-05-08 16:54:20 +08:00
|
|
|
|
2008-05-25 13:15:42 +08:00
|
|
|
if (DI) {
|
2008-11-12 07:11:34 +08:00
|
|
|
EnsureInsertPoint();
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(S.getRBracLoc());
|
2008-05-25 13:15:42 +08:00
|
|
|
DI->EmitRegionEnd(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2008-12-12 13:52:00 +08:00
|
|
|
RValue RV;
|
|
|
|
if (!GetLast)
|
|
|
|
RV = RValue::get(0);
|
|
|
|
else {
|
|
|
|
// We have to special case labels here. They are statements, but when put
|
|
|
|
// at the end of a statement expression, they yield the value of their
|
|
|
|
// subexpression. Handle this by walking through all labels we encounter,
|
|
|
|
// emitting them before we evaluate the subexpr.
|
|
|
|
const Stmt *LastStmt = S.body_back();
|
|
|
|
while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
|
|
|
|
EmitLabel(*LS);
|
|
|
|
LastStmt = LS->getSubStmt();
|
|
|
|
}
|
2007-09-01 06:49:20 +08:00
|
|
|
|
2008-12-12 13:52:00 +08:00
|
|
|
EnsureInsertPoint();
|
|
|
|
|
|
|
|
RV = EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
|
2008-07-27 04:23:23 +08:00
|
|
|
}
|
2008-11-12 07:11:34 +08:00
|
|
|
|
2008-12-12 13:52:00 +08:00
|
|
|
if (llvm::Value *V = StackSaveValues.pop_back_val()) {
|
2009-02-07 20:52:26 +08:00
|
|
|
StackDepth = V;
|
2008-12-12 13:52:00 +08:00
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
|
|
|
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
|
|
|
|
Builder.CreateCall(F, V);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RV;
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
2008-11-13 09:24:05 +08:00
|
|
|
void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
|
2008-11-11 17:41:28 +08:00
|
|
|
// Fall out of the current block (if necessary).
|
|
|
|
EmitBranch(BB);
|
2008-11-13 09:24:05 +08:00
|
|
|
|
|
|
|
if (IsFinished && BB->use_empty()) {
|
|
|
|
delete BB;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:41:28 +08:00
|
|
|
CurFn->getBasicBlockList().push_back(BB);
|
|
|
|
Builder.SetInsertPoint(BB);
|
|
|
|
}
|
|
|
|
|
2009-02-07 20:52:26 +08:00
|
|
|
void CodeGenFunction::EmitStackUpdate(const LabelStmt &S) {
|
|
|
|
if (StackDepthMap.find(&S) == StackDepthMap.end()) {
|
|
|
|
// If we can't find it, just remember the depth now,
|
|
|
|
// so we can validate it later.
|
|
|
|
// FIXME: We need to save a place to insert the adjustment,
|
|
|
|
// if needed, here, sa that later in EmitLabel, we can
|
|
|
|
// backpatch the adjustment into that place, instead of
|
|
|
|
// saying unsupported.
|
|
|
|
StackDepthMap[&S] = StackDepth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find applicable stack depth, if any...
|
|
|
|
llvm::Value *V = StackDepthMap[&S];
|
|
|
|
// V can be 0 here, if it is, be sure to start searching from the
|
|
|
|
// top of the function, as we want the next save after that point.
|
|
|
|
for (unsigned int i = 0; i < StackSaveValues.size(); ++i)
|
|
|
|
if (StackSaveValues[i] == V) {
|
|
|
|
// The actual depth is actually in the next used slot, if any.
|
|
|
|
while (++i < StackSaveValues.size()
|
|
|
|
&& (V = StackSaveValues[i]) == 0) ;
|
|
|
|
// If there were no other depth changes, we don't need any
|
|
|
|
// adjustments.
|
|
|
|
if (V) {
|
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
|
|
|
// and restore it.
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
|
|
|
|
Builder.CreateCall(F, V);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
// FIXME: Move to semq and assert here, codegen isn't the right
|
|
|
|
// time to be checking.
|
|
|
|
CGM.ErrorUnsupported(&S, "invalid goto to VLA scope that has finished");
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:41:28 +08:00
|
|
|
void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
|
|
|
|
// Emit a branch from the current block to the target one if this
|
|
|
|
// was a real block. If this was just a fall-through block after a
|
|
|
|
// terminator, don't emit it.
|
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
if (!CurBB || CurBB->getTerminator()) {
|
|
|
|
// If there is no insert point or the previous block is already
|
|
|
|
// terminated, don't touch it.
|
2007-06-02 02:02:12 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, create a fall-through branch.
|
2008-11-11 17:41:28 +08:00
|
|
|
Builder.CreateBr(Target);
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
2008-11-12 06:06:59 +08:00
|
|
|
|
|
|
|
Builder.ClearInsertionPoint();
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
2008-07-27 04:23:23 +08:00
|
|
|
void CodeGenFunction::EmitLabel(const LabelStmt &S) {
|
2007-06-02 02:02:12 +08:00
|
|
|
llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
|
2009-02-07 20:52:26 +08:00
|
|
|
if (StackDepthMap.find(&S) == StackDepthMap.end()) {
|
|
|
|
// We need to remember the stack depth so that we can readjust the
|
|
|
|
// stack back to the right depth for this label if we want to
|
|
|
|
// transfer here from a different depth.
|
|
|
|
StackDepthMap[&S] = StackDepth;
|
|
|
|
} else {
|
|
|
|
if (StackDepthMap[&S] != StackDepth) {
|
|
|
|
// FIXME: Sema needs to ckeck for jumps that cross decls with
|
|
|
|
// initializations for C++, and all VLAs, not just the first in
|
|
|
|
// a block that does a stacksave.
|
|
|
|
// FIXME: We need to save a place to insert the adjustment
|
|
|
|
// when we do a EmitStackUpdate on a forward jump, and then
|
|
|
|
// backpatch the adjustment into that place.
|
|
|
|
CGM.ErrorUnsupported(&S, "forward goto inside scope with VLA");
|
|
|
|
}
|
|
|
|
}
|
2007-06-02 02:02:12 +08:00
|
|
|
EmitBlock(NextBB);
|
2008-07-27 04:23:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
|
|
|
|
EmitLabel(S);
|
2007-06-02 02:02:12 +08:00
|
|
|
EmitStmt(S.getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
|
2008-10-03 02:02:06 +08:00
|
|
|
// FIXME: Implement goto out in @try or @catch blocks.
|
|
|
|
if (!ObjCEHStack.empty()) {
|
|
|
|
CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// If this code is reachable then emit a stop point (if generating
|
|
|
|
// debug info). We have to do this ourselves because we are on the
|
|
|
|
// "simple" statement path.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
EmitStopPoint(&S);
|
2009-02-07 20:52:26 +08:00
|
|
|
|
|
|
|
// We need to adjust the stack, if the destination was (will be) at
|
|
|
|
// a different depth.
|
|
|
|
EmitStackUpdate(*S.getLabel());
|
|
|
|
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(getBasicBlockForLabel(S.getLabel()));
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
|
2008-10-03 02:02:06 +08:00
|
|
|
// FIXME: Implement indirect goto in @try or @catch blocks.
|
|
|
|
if (!ObjCEHStack.empty()) {
|
|
|
|
CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
// Emit initial switch which will be patched up later by
|
|
|
|
// EmitIndirectSwitches(). We need a default dest, so we use the
|
|
|
|
// current BB, but this is overwritten.
|
|
|
|
llvm::Value *V = Builder.CreatePtrToInt(EmitScalarExpr(S.getTarget()),
|
|
|
|
llvm::Type::Int32Ty,
|
|
|
|
"addr");
|
|
|
|
llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock());
|
|
|
|
IndirectSwitches.push_back(I);
|
|
|
|
|
2008-11-12 07:11:34 +08:00
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
2008-08-05 00:51:22 +08:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:24:28 +08:00
|
|
|
void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
|
2007-06-02 02:02:12 +08:00
|
|
|
// C99 6.8.4.1: The first substatement is executed if the expression compares
|
|
|
|
// unequal to 0. The condition must be a scalar type.
|
|
|
|
|
Make emission of 'if' conditions much more sophisticated when we
have a condition that is an &&/||. Before we used to compile things like this:
int test() {
if (x && y) foo(); else bar();
}
into:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_rhs, label %land_cont
land_rhs: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br label %land_cont
land_cont: ; preds = %land_rhs, %entry
%4 = phi i1 [ false, %entry ], [ %3, %land_rhs ] ; <i1> [#uses=1]
br i1 %4, label %ifthen, label %ifelse
ifthen: ; preds = %land_cont
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_cont
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Now we turn it into the much more svelte code:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_lhs_true, label %ifelse
land_lhs_true: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br i1 %3, label %ifthen, label %ifelse
ifthen: ; preds = %land_lhs_true
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_lhs_true, %entry
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Note the lack of a phi node.
This shrinks the -O0 .ll file for 176.gcc/expr.c from 43176 to 40267 lines.
llvm-svn: 59111
2008-11-12 15:46:33 +08:00
|
|
|
// If the condition constant folds and can be elided, try to avoid emitting
|
|
|
|
// the condition and the dead arm of the if/else.
|
2008-11-12 16:04:58 +08:00
|
|
|
if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
|
2008-11-11 15:24:28 +08:00
|
|
|
// Figure out which block (then or else) is executed.
|
|
|
|
const Stmt *Executed = S.getThen(), *Skipped = S.getElse();
|
Make emission of 'if' conditions much more sophisticated when we
have a condition that is an &&/||. Before we used to compile things like this:
int test() {
if (x && y) foo(); else bar();
}
into:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_rhs, label %land_cont
land_rhs: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br label %land_cont
land_cont: ; preds = %land_rhs, %entry
%4 = phi i1 [ false, %entry ], [ %3, %land_rhs ] ; <i1> [#uses=1]
br i1 %4, label %ifthen, label %ifelse
ifthen: ; preds = %land_cont
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_cont
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Now we turn it into the much more svelte code:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_lhs_true, label %ifelse
land_lhs_true: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br i1 %3, label %ifthen, label %ifelse
ifthen: ; preds = %land_lhs_true
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_lhs_true, %entry
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Note the lack of a phi node.
This shrinks the -O0 .ll file for 176.gcc/expr.c from 43176 to 40267 lines.
llvm-svn: 59111
2008-11-12 15:46:33 +08:00
|
|
|
if (Cond == -1) // Condition false?
|
2008-11-11 15:24:28 +08:00
|
|
|
std::swap(Executed, Skipped);
|
Make emission of 'if' conditions much more sophisticated when we
have a condition that is an &&/||. Before we used to compile things like this:
int test() {
if (x && y) foo(); else bar();
}
into:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_rhs, label %land_cont
land_rhs: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br label %land_cont
land_cont: ; preds = %land_rhs, %entry
%4 = phi i1 [ false, %entry ], [ %3, %land_rhs ] ; <i1> [#uses=1]
br i1 %4, label %ifthen, label %ifelse
ifthen: ; preds = %land_cont
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_cont
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Now we turn it into the much more svelte code:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_lhs_true, label %ifelse
land_lhs_true: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br i1 %3, label %ifthen, label %ifelse
ifthen: ; preds = %land_lhs_true
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_lhs_true, %entry
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Note the lack of a phi node.
This shrinks the -O0 .ll file for 176.gcc/expr.c from 43176 to 40267 lines.
llvm-svn: 59111
2008-11-12 15:46:33 +08:00
|
|
|
|
2008-11-11 15:24:28 +08:00
|
|
|
// If the skipped block has no labels in it, just emit the executed block.
|
|
|
|
// This avoids emitting dead code and simplifies the CFG substantially.
|
Make emission of 'if' conditions much more sophisticated when we
have a condition that is an &&/||. Before we used to compile things like this:
int test() {
if (x && y) foo(); else bar();
}
into:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_rhs, label %land_cont
land_rhs: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br label %land_cont
land_cont: ; preds = %land_rhs, %entry
%4 = phi i1 [ false, %entry ], [ %3, %land_rhs ] ; <i1> [#uses=1]
br i1 %4, label %ifthen, label %ifelse
ifthen: ; preds = %land_cont
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_cont
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Now we turn it into the much more svelte code:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_lhs_true, label %ifelse
land_lhs_true: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br i1 %3, label %ifthen, label %ifelse
ifthen: ; preds = %land_lhs_true
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_lhs_true, %entry
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Note the lack of a phi node.
This shrinks the -O0 .ll file for 176.gcc/expr.c from 43176 to 40267 lines.
llvm-svn: 59111
2008-11-12 15:46:33 +08:00
|
|
|
if (!ContainsLabel(Skipped)) {
|
2008-11-11 15:24:28 +08:00
|
|
|
if (Executed)
|
|
|
|
EmitStmt(Executed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
Make emission of 'if' conditions much more sophisticated when we
have a condition that is an &&/||. Before we used to compile things like this:
int test() {
if (x && y) foo(); else bar();
}
into:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_rhs, label %land_cont
land_rhs: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br label %land_cont
land_cont: ; preds = %land_rhs, %entry
%4 = phi i1 [ false, %entry ], [ %3, %land_rhs ] ; <i1> [#uses=1]
br i1 %4, label %ifthen, label %ifelse
ifthen: ; preds = %land_cont
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_cont
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Now we turn it into the much more svelte code:
%0 = load i32* @x ; <i32> [#uses=1]
%1 = icmp ne i32 %0, 0 ; <i1> [#uses=1]
br i1 %1, label %land_lhs_true, label %ifelse
land_lhs_true: ; preds = %entry
%2 = load i32* @y ; <i32> [#uses=1]
%3 = icmp ne i32 %2, 0 ; <i1> [#uses=1]
br i1 %3, label %ifthen, label %ifelse
ifthen: ; preds = %land_lhs_true
%call = call i32 (...)* @foo() ; <i32> [#uses=0]
br label %ifend
ifelse: ; preds = %land_lhs_true, %entry
%call1 = call i32 (...)* @bar() ; <i32> [#uses=0]
br label %ifend
ifend: ; preds = %ifelse, %ifthen
Note the lack of a phi node.
This shrinks the -O0 .ll file for 176.gcc/expr.c from 43176 to 40267 lines.
llvm-svn: 59111
2008-11-12 15:46:33 +08:00
|
|
|
|
|
|
|
// Otherwise, the condition did not fold, or we couldn't elide it. Just emit
|
|
|
|
// the conditional branch.
|
2008-11-13 08:47:57 +08:00
|
|
|
llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
|
|
|
|
llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
|
|
|
|
llvm::BasicBlock *ElseBlock = ContBlock;
|
2007-06-02 02:02:12 +08:00
|
|
|
if (S.getElse())
|
2008-11-13 08:47:57 +08:00
|
|
|
ElseBlock = createBasicBlock("if.else");
|
|
|
|
EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
|
2007-06-02 02:02:12 +08:00
|
|
|
|
|
|
|
// Emit the 'then' code.
|
|
|
|
EmitBlock(ThenBlock);
|
|
|
|
EmitStmt(S.getThen());
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(ContBlock);
|
2007-06-02 02:02:12 +08:00
|
|
|
|
|
|
|
// Emit the 'else' code if present.
|
|
|
|
if (const Stmt *Else = S.getElse()) {
|
|
|
|
EmitBlock(ElseBlock);
|
|
|
|
EmitStmt(Else);
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(ContBlock);
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the continuation block for code after the if.
|
2008-11-13 09:54:24 +08:00
|
|
|
EmitBlock(ContBlock, true);
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
2007-06-05 11:59:43 +08:00
|
|
|
void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
|
|
|
|
// Emit the header for the loop, insert it, which will create an uncond br to
|
|
|
|
// it.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LoopHeader = createBasicBlock("while.cond");
|
2007-06-05 11:59:43 +08:00
|
|
|
EmitBlock(LoopHeader);
|
2009-02-08 02:08:12 +08:00
|
|
|
|
|
|
|
// Create an exit block for when the condition fails, create a block for the
|
|
|
|
// body of the loop.
|
|
|
|
llvm::BasicBlock *ExitBlock = createBasicBlock("while.end");
|
|
|
|
llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
|
|
|
|
|
|
|
|
// Store the blocks to use for break and continue.
|
|
|
|
BreakContinuePush(ExitBlock, LoopHeader);
|
2007-06-05 11:59:43 +08:00
|
|
|
|
2009-02-08 01:18:33 +08:00
|
|
|
// Evaluate the conditional in the while header. C99 6.8.5.1: The
|
|
|
|
// evaluation of the controlling expression takes place before each
|
|
|
|
// execution of the loop body.
|
2007-06-06 04:53:16 +08:00
|
|
|
llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
|
2007-10-10 04:51:27 +08:00
|
|
|
|
|
|
|
// while(1) is common, avoid extra exit blocks. Be sure
|
2007-06-06 04:53:16 +08:00
|
|
|
// to correctly handle break/continue though.
|
2007-10-10 04:51:27 +08:00
|
|
|
bool EmitBoolCondBranch = true;
|
|
|
|
if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
|
|
|
|
if (C->isOne())
|
|
|
|
EmitBoolCondBranch = false;
|
2007-06-05 11:59:43 +08:00
|
|
|
|
|
|
|
// As long as the condition is true, go to the loop body.
|
2007-10-10 04:51:27 +08:00
|
|
|
if (EmitBoolCondBranch)
|
|
|
|
Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
|
2008-11-12 16:04:58 +08:00
|
|
|
|
2007-06-05 11:59:43 +08:00
|
|
|
// Emit the loop body.
|
|
|
|
EmitBlock(LoopBody);
|
|
|
|
EmitStmt(S.getBody());
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePop();
|
2007-06-05 11:59:43 +08:00
|
|
|
|
|
|
|
// Cycle to the condition.
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(LoopHeader);
|
2007-06-05 11:59:43 +08:00
|
|
|
|
|
|
|
// Emit the exit block.
|
2008-11-13 09:54:24 +08:00
|
|
|
EmitBlock(ExitBlock, true);
|
2007-10-10 04:51:27 +08:00
|
|
|
|
|
|
|
// If LoopHeader is a simple forwarding block then eliminate it.
|
|
|
|
if (!EmitBoolCondBranch
|
|
|
|
&& &LoopHeader->front() == LoopHeader->getTerminator()) {
|
|
|
|
LoopHeader->replaceAllUsesWith(LoopBody);
|
|
|
|
LoopHeader->getTerminator()->eraseFromParent();
|
|
|
|
LoopHeader->eraseFromParent();
|
|
|
|
}
|
2007-06-05 11:59:43 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
|
|
|
|
// Emit the body for the loop, insert it, which will create an uncond br to
|
|
|
|
// it.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
|
|
|
|
llvm::BasicBlock *AfterDo = createBasicBlock("do.end");
|
2007-06-06 04:53:16 +08:00
|
|
|
EmitBlock(LoopBody);
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *DoCond = createBasicBlock("do.cond");
|
2007-07-17 05:28:45 +08:00
|
|
|
|
|
|
|
// Store the blocks to use for break and continue.
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePush(AfterDo, DoCond);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Emit the body of the loop into the block.
|
|
|
|
EmitStmt(S.getBody());
|
|
|
|
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePop();
|
2007-07-17 05:28:45 +08:00
|
|
|
|
|
|
|
EmitBlock(DoCond);
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// C99 6.8.5.2: "The evaluation of the controlling expression takes place
|
|
|
|
// after each execution of the loop body."
|
|
|
|
|
|
|
|
// Evaluate the conditional in the while header.
|
|
|
|
// C99 6.8.5p2/p4: The first substatement is executed if the expression
|
|
|
|
// compares unequal to 0. The condition must be a scalar type.
|
|
|
|
llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
|
2007-10-10 04:33:39 +08:00
|
|
|
|
|
|
|
// "do {} while (0)" is common in macros, avoid extra blocks. Be sure
|
|
|
|
// to correctly handle break/continue though.
|
|
|
|
bool EmitBoolCondBranch = true;
|
|
|
|
if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
|
|
|
|
if (C->isZero())
|
|
|
|
EmitBoolCondBranch = false;
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// As long as the condition is true, iterate the loop.
|
2007-10-10 04:33:39 +08:00
|
|
|
if (EmitBoolCondBranch)
|
|
|
|
Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Emit the exit block.
|
2008-11-13 09:54:24 +08:00
|
|
|
EmitBlock(AfterDo, true);
|
2007-10-10 04:33:39 +08:00
|
|
|
|
|
|
|
// If DoCond is a simple forwarding block then eliminate it.
|
|
|
|
if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
|
|
|
|
DoCond->replaceAllUsesWith(AfterDo);
|
|
|
|
DoCond->getTerminator()->eraseFromParent();
|
|
|
|
DoCond->eraseFromParent();
|
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitForStmt(const ForStmt &S) {
|
|
|
|
// FIXME: What do we do if the increment (f.e.) contains a stmt expression,
|
|
|
|
// which contains a continue/break?
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// Evaluate the first part before the loop.
|
|
|
|
if (S.getInit())
|
|
|
|
EmitStmt(S.getInit());
|
|
|
|
|
|
|
|
// Start the loop with a block that tests the condition.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
|
|
|
|
llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
EmitBlock(CondBlock);
|
|
|
|
|
2009-02-08 04:14:12 +08:00
|
|
|
// Evaluate the condition if present. If not, treat it as a
|
|
|
|
// non-zero-constant according to 6.8.5.3p2, aka, true.
|
2007-06-06 04:53:16 +08:00
|
|
|
if (S.getCond()) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// As long as the condition is true, iterate the loop.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
|
2008-11-12 16:04:58 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// C99 6.8.5p2/p4: The first substatement is executed if the expression
|
|
|
|
// compares unequal to 0. The condition must be a scalar type.
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(S.getCond(), ForBody, AfterFor);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
EmitBlock(ForBody);
|
|
|
|
} else {
|
|
|
|
// Treat it as a non-zero constant. Don't even create a new block for the
|
|
|
|
// body, just fall into it.
|
|
|
|
}
|
|
|
|
|
2007-07-17 05:28:45 +08:00
|
|
|
// If the for loop doesn't have an increment we can just use the
|
|
|
|
// condition as the continue block.
|
|
|
|
llvm::BasicBlock *ContinueBlock;
|
|
|
|
if (S.getInc())
|
2008-11-13 09:38:36 +08:00
|
|
|
ContinueBlock = createBasicBlock("for.inc");
|
2007-07-17 05:28:45 +08:00
|
|
|
else
|
|
|
|
ContinueBlock = CondBlock;
|
|
|
|
|
|
|
|
// Store the blocks to use for break and continue.
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePush(AfterFor, ContinueBlock);
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// If the condition is true, execute the body of the for stmt.
|
|
|
|
EmitStmt(S.getBody());
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePop();
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// If there is an increment, emit it next.
|
2008-09-28 08:19:22 +08:00
|
|
|
if (S.getInc()) {
|
|
|
|
EmitBlock(ContinueBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
EmitStmt(S.getInc());
|
2008-09-28 08:19:22 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Finally, branch back up to the condition for the next iteration.
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(CondBlock);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-07-17 05:28:45 +08:00
|
|
|
// Emit the fall-through block.
|
2008-11-13 09:54:24 +08:00
|
|
|
EmitBlock(AfterFor, true);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
2007-06-05 11:59:43 +08:00
|
|
|
|
2008-09-24 12:00:38 +08:00
|
|
|
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
|
|
|
|
if (RV.isScalar()) {
|
|
|
|
Builder.CreateStore(RV.getScalarVal(), ReturnValue);
|
|
|
|
} else if (RV.isAggregate()) {
|
|
|
|
EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
|
|
|
|
} else {
|
|
|
|
StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
|
|
|
|
}
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(ReturnBlock);
|
2008-09-24 12:00:38 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 11:19:07 +08:00
|
|
|
/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
|
|
|
|
/// if the function returns void, or may be missing one if the function returns
|
|
|
|
/// non-void. Fun stuff :).
|
|
|
|
void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
|
2008-12-21 07:18:29 +08:00
|
|
|
for (unsigned i = 0; i < StackSaveValues.size(); i++) {
|
2008-12-21 05:33:38 +08:00
|
|
|
if (StackSaveValues[i]) {
|
|
|
|
CGM.ErrorUnsupported(&S, "return inside scope with VLA");
|
|
|
|
return;
|
|
|
|
}
|
2008-12-21 03:33:21 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 11:19:07 +08:00
|
|
|
// Emit the result value, even if unused, to evalute the side effects.
|
|
|
|
const Expr *RV = S.getRetValue();
|
2008-09-10 05:00:17 +08:00
|
|
|
|
|
|
|
// FIXME: Clean this up by using an LValue for ReturnTemp,
|
|
|
|
// EmitStoreThroughLValue, and EmitAnyExpr.
|
|
|
|
if (!ReturnValue) {
|
|
|
|
// Make sure not to return anything, but evaluate the expression
|
|
|
|
// for side effects.
|
|
|
|
if (RV)
|
2008-05-22 09:22:33 +08:00
|
|
|
EmitAnyExpr(RV);
|
2007-06-02 11:19:07 +08:00
|
|
|
} else if (RV == 0) {
|
2008-09-10 05:00:17 +08:00
|
|
|
// Do nothing (return value is left uninitialized)
|
2007-08-26 15:14:44 +08:00
|
|
|
} else if (!hasAggregateLLVMType(RV->getType())) {
|
2008-09-10 05:00:17 +08:00
|
|
|
Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
|
2008-04-05 00:54:41 +08:00
|
|
|
} else if (RV->getType()->isAnyComplexType()) {
|
2008-09-10 05:00:17 +08:00
|
|
|
EmitComplexExprIntoAddr(RV, ReturnValue, false);
|
2007-06-02 11:19:07 +08:00
|
|
|
} else {
|
2008-09-10 05:00:17 +08:00
|
|
|
EmitAggExpr(RV, ReturnValue, false);
|
2007-06-02 11:19:07 +08:00
|
|
|
}
|
2008-05-22 09:22:33 +08:00
|
|
|
|
2008-09-30 09:06:03 +08:00
|
|
|
if (!ObjCEHStack.empty()) {
|
|
|
|
for (ObjCEHStackType::reverse_iterator i = ObjCEHStack.rbegin(),
|
|
|
|
e = ObjCEHStack.rend(); i != e; ++i) {
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *ReturnPad = createBasicBlock("return.pad");
|
2008-09-30 09:06:03 +08:00
|
|
|
EmitJumpThroughFinally(*i, ReturnPad);
|
|
|
|
EmitBlock(ReturnPad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(ReturnBlock);
|
2007-06-02 11:19:07 +08:00
|
|
|
}
|
|
|
|
|
2007-06-09 09:20:56 +08:00
|
|
|
void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
|
2008-10-07 02:42:27 +08:00
|
|
|
for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
|
|
|
|
I != E; ++I)
|
|
|
|
EmitDecl(**I);
|
2007-07-12 23:43:07 +08:00
|
|
|
}
|
2007-07-17 05:28:45 +08:00
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
|
2007-07-17 05:28:45 +08:00
|
|
|
assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// FIXME: Implement break in @try or @catch blocks.
|
2008-12-14 06:52:24 +08:00
|
|
|
if (ObjCEHStack.size() != BreakContinueStack.back().EHStackSize) {
|
2008-12-21 03:33:21 +08:00
|
|
|
CGM.ErrorUnsupported(&S, "break inside an Obj-C exception block");
|
2008-11-12 16:21:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-21 07:18:29 +08:00
|
|
|
for (unsigned i = 0; i < StackSaveValues.size(); i++) {
|
|
|
|
if (StackSaveValues[i]) {
|
|
|
|
CGM.ErrorUnsupported(&S, "break inside scope with VLA");
|
|
|
|
return;
|
|
|
|
}
|
2008-12-21 03:33:21 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// If this code is reachable then emit a stop point (if generating
|
|
|
|
// debug info). We have to do this ourselves because we are on the
|
|
|
|
// "simple" statement path.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
EmitStopPoint(&S);
|
2007-07-17 05:28:45 +08:00
|
|
|
llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(Block);
|
2007-07-17 05:28:45 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
|
2007-07-17 05:28:45 +08:00
|
|
|
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// FIXME: Implement continue in @try or @catch blocks.
|
2008-12-14 06:52:24 +08:00
|
|
|
if (ObjCEHStack.size() != BreakContinueStack.back().EHStackSize) {
|
2008-11-12 16:21:33 +08:00
|
|
|
CGM.ErrorUnsupported(&S, "continue inside an Obj-C exception block");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-21 07:18:29 +08:00
|
|
|
for (unsigned i = 0; i < StackSaveValues.size(); i++) {
|
|
|
|
if (StackSaveValues[i]) {
|
|
|
|
CGM.ErrorUnsupported(&S, "continue inside scope with VLA");
|
|
|
|
return;
|
|
|
|
}
|
2008-12-21 03:33:21 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// If this code is reachable then emit a stop point (if generating
|
|
|
|
// debug info). We have to do this ourselves because we are on the
|
|
|
|
// "simple" statement path.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
EmitStopPoint(&S);
|
2007-07-17 05:28:45 +08:00
|
|
|
llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
|
2008-11-11 17:41:28 +08:00
|
|
|
EmitBranch(Block);
|
2007-07-17 05:28:45 +08:00
|
|
|
}
|
2007-10-05 07:45:31 +08:00
|
|
|
|
2007-10-09 04:57:48 +08:00
|
|
|
/// EmitCaseStmtRange - If case statement range is not too big then
|
|
|
|
/// add multiple cases to switch instruction, one for each value within
|
|
|
|
/// the range. If range is too big then emit "if" condition check.
|
|
|
|
void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
|
2008-07-24 09:18:41 +08:00
|
|
|
assert(S.getRHS() && "Expected RHS value in CaseStmt");
|
2007-10-09 04:57:48 +08:00
|
|
|
|
2008-11-23 05:04:56 +08:00
|
|
|
llvm::APSInt LHS = S.getLHS()->EvaluateAsInt(getContext());
|
|
|
|
llvm::APSInt RHS = S.getRHS()->EvaluateAsInt(getContext());
|
2008-07-24 09:18:41 +08:00
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// Emit the code for this case. We do this first to make sure it is
|
|
|
|
// properly chained from our predecessor before generating the
|
|
|
|
// switch machinery to enter this block.
|
2008-11-11 12:12:31 +08:00
|
|
|
EmitBlock(createBasicBlock("sw.bb"));
|
2008-07-25 09:11:38 +08:00
|
|
|
llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
|
|
|
|
EmitStmt(S.getSubStmt());
|
|
|
|
|
2008-07-24 09:18:41 +08:00
|
|
|
// If range is empty, do nothing.
|
|
|
|
if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
|
|
|
|
return;
|
2007-10-09 04:57:48 +08:00
|
|
|
|
|
|
|
llvm::APInt Range = RHS - LHS;
|
2008-07-25 09:11:38 +08:00
|
|
|
// FIXME: parameters such as this should not be hardcoded.
|
2007-10-09 04:57:48 +08:00
|
|
|
if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
|
|
|
|
// Range is small enough to add multiple switch instruction cases.
|
2008-07-24 09:18:41 +08:00
|
|
|
for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
|
2007-10-06 04:54:07 +08:00
|
|
|
SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
|
|
|
|
LHS++;
|
|
|
|
}
|
2007-10-09 04:57:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// The range is too big. Emit "if" condition into a new block,
|
|
|
|
// making sure to save and restore the current insertion point.
|
|
|
|
llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
// Push this test onto the chain of range checks (which terminates
|
|
|
|
// in the default basic block). The switch's default will be changed
|
|
|
|
// to the top of this chain after switch emission is complete.
|
|
|
|
llvm::BasicBlock *FalseDest = CaseRangeBlock;
|
2008-11-11 10:29:29 +08:00
|
|
|
CaseRangeBlock = createBasicBlock("sw.caserange");
|
2008-07-25 09:11:38 +08:00
|
|
|
|
|
|
|
CurFn->getBasicBlockList().push_back(CaseRangeBlock);
|
|
|
|
Builder.SetInsertPoint(CaseRangeBlock);
|
2007-10-09 04:57:48 +08:00
|
|
|
|
|
|
|
// Emit range check.
|
|
|
|
llvm::Value *Diff =
|
2008-07-24 09:18:41 +08:00
|
|
|
Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS),
|
|
|
|
"tmp");
|
2007-10-09 04:57:48 +08:00
|
|
|
llvm::Value *Cond =
|
|
|
|
Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
|
|
|
|
Builder.CreateCondBr(Cond, CaseDest, FalseDest);
|
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// Restore the appropriate insertion point.
|
2008-11-12 07:11:34 +08:00
|
|
|
if (RestoreBB)
|
|
|
|
Builder.SetInsertPoint(RestoreBB);
|
|
|
|
else
|
|
|
|
Builder.ClearInsertionPoint();
|
2007-10-09 04:57:48 +08:00
|
|
|
}
|
2007-10-06 04:54:07 +08:00
|
|
|
|
2007-10-09 04:57:48 +08:00
|
|
|
void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
|
|
|
|
if (S.getRHS()) {
|
|
|
|
EmitCaseStmtRange(S);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-11 12:12:31 +08:00
|
|
|
EmitBlock(createBasicBlock("sw.bb"));
|
2007-10-09 04:57:48 +08:00
|
|
|
llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
|
2008-11-23 05:04:56 +08:00
|
|
|
llvm::APSInt CaseVal = S.getLHS()->EvaluateAsInt(getContext());
|
2008-11-11 10:29:29 +08:00
|
|
|
SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal), CaseDest);
|
2007-10-05 07:45:31 +08:00
|
|
|
EmitStmt(S.getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
|
2008-07-25 09:11:38 +08:00
|
|
|
llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
|
2008-11-11 10:29:29 +08:00
|
|
|
assert(DefaultBlock->empty() &&
|
|
|
|
"EmitDefaultStmt: Default block already defined?");
|
2008-07-25 09:11:38 +08:00
|
|
|
EmitBlock(DefaultBlock);
|
2007-10-05 07:45:31 +08:00
|
|
|
EmitStmt(S.getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
|
|
|
|
llvm::Value *CondV = EmitScalarExpr(S.getCond());
|
|
|
|
|
|
|
|
// Handle nested switch statements.
|
|
|
|
llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
|
2007-10-09 04:57:48 +08:00
|
|
|
llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
|
2007-10-05 07:45:31 +08:00
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// Create basic block to hold stuff that comes after switch
|
|
|
|
// statement. We also need to create a default block now so that
|
|
|
|
// explicit case ranges tests can have a place to jump to on
|
|
|
|
// failure.
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *NextBlock = createBasicBlock("sw.epilog");
|
|
|
|
llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
|
2008-07-25 09:11:38 +08:00
|
|
|
SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
|
|
|
|
CaseRangeBlock = DefaultBlock;
|
2007-10-05 07:45:31 +08:00
|
|
|
|
2008-11-12 16:21:33 +08:00
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
2008-05-13 00:08:04 +08:00
|
|
|
|
2007-10-31 04:59:40 +08:00
|
|
|
// All break statements jump to NextBlock. If BreakContinueStack is non empty
|
|
|
|
// then reuse last ContinueBlock.
|
2007-10-05 07:45:31 +08:00
|
|
|
llvm::BasicBlock *ContinueBlock = NULL;
|
|
|
|
if (!BreakContinueStack.empty())
|
|
|
|
ContinueBlock = BreakContinueStack.back().ContinueBlock;
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePush(NextBlock, ContinueBlock);
|
2007-10-05 07:45:31 +08:00
|
|
|
|
|
|
|
// Emit switch body.
|
|
|
|
EmitStmt(S.getBody());
|
2009-02-07 20:52:26 +08:00
|
|
|
BreakContinuePop();
|
2007-10-05 07:45:31 +08:00
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// Update the default block in case explicit case range tests have
|
|
|
|
// been chained on top.
|
|
|
|
SwitchInsn->setSuccessor(0, CaseRangeBlock);
|
2007-10-09 04:57:48 +08:00
|
|
|
|
2008-07-25 09:11:38 +08:00
|
|
|
// If a default was never emitted then reroute any jumps to it and
|
|
|
|
// discard.
|
|
|
|
if (!DefaultBlock->getParent()) {
|
|
|
|
DefaultBlock->replaceAllUsesWith(NextBlock);
|
|
|
|
delete DefaultBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit continuation.
|
2008-11-13 09:54:24 +08:00
|
|
|
EmitBlock(NextBlock, true);
|
2007-10-05 07:45:31 +08:00
|
|
|
|
|
|
|
SwitchInsn = SavedSwitchInsn;
|
2007-10-09 04:57:48 +08:00
|
|
|
CaseRangeBlock = SavedCRBlock;
|
2007-10-05 07:45:31 +08:00
|
|
|
}
|
2008-02-06 00:35:33 +08:00
|
|
|
|
2008-11-10 02:54:14 +08:00
|
|
|
static std::string ConvertAsmString(const AsmStmt& S, bool &Failed)
|
|
|
|
{
|
|
|
|
// FIXME: No need to create new std::string here, we could just make sure
|
|
|
|
// that we don't read past the end of the string data.
|
|
|
|
std::string str(S.getAsmString()->getStrData(),
|
|
|
|
S.getAsmString()->getByteLength());
|
|
|
|
const char *Start = str.c_str();
|
|
|
|
|
|
|
|
unsigned NumOperands = S.getNumOutputs() + S.getNumInputs();
|
|
|
|
bool IsSimple = S.isSimple();
|
2008-10-18 04:58:01 +08:00
|
|
|
Failed = false;
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
static unsigned AsmCounter = 0;
|
|
|
|
AsmCounter++;
|
|
|
|
std::string Result;
|
2008-02-06 07:18:57 +08:00
|
|
|
if (IsSimple) {
|
|
|
|
while (*Start) {
|
|
|
|
switch (*Start) {
|
|
|
|
default:
|
|
|
|
Result += *Start;
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
Result += "$$";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
2008-02-06 00:35:33 +08:00
|
|
|
|
|
|
|
while (*Start) {
|
|
|
|
switch (*Start) {
|
|
|
|
default:
|
|
|
|
Result += *Start;
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
Result += "$$";
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
// Escaped character
|
|
|
|
Start++;
|
|
|
|
if (!*Start) {
|
|
|
|
// FIXME: This should be caught during Sema.
|
|
|
|
assert(0 && "Trailing '%' in asm string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
char EscapedChar = *Start;
|
|
|
|
if (EscapedChar == '%') {
|
|
|
|
// Escaped percentage sign.
|
|
|
|
Result += '%';
|
2008-07-27 04:15:14 +08:00
|
|
|
} else if (EscapedChar == '=') {
|
2008-02-06 00:35:33 +08:00
|
|
|
// Generate an unique ID.
|
|
|
|
Result += llvm::utostr(AsmCounter);
|
|
|
|
} else if (isdigit(EscapedChar)) {
|
|
|
|
// %n - Assembler operand n
|
|
|
|
char *End;
|
|
|
|
unsigned long n = strtoul(Start, &End, 10);
|
|
|
|
if (Start == End) {
|
|
|
|
// FIXME: This should be caught during Sema.
|
|
|
|
assert(0 && "Missing operand!");
|
|
|
|
} else if (n >= NumOperands) {
|
|
|
|
// FIXME: This should be caught during Sema.
|
|
|
|
assert(0 && "Operand number out of range!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Result += '$' + llvm::utostr(n);
|
2008-02-27 03:19:58 +08:00
|
|
|
Start = End - 1;
|
2008-02-06 07:18:57 +08:00
|
|
|
} else if (isalpha(EscapedChar)) {
|
|
|
|
char *End;
|
|
|
|
|
|
|
|
unsigned long n = strtoul(Start + 1, &End, 10);
|
|
|
|
if (Start == End) {
|
|
|
|
// FIXME: This should be caught during Sema.
|
|
|
|
assert(0 && "Missing operand!");
|
|
|
|
} else if (n >= NumOperands) {
|
|
|
|
// FIXME: This should be caught during Sema.
|
|
|
|
assert(0 && "Operand number out of range!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}';
|
2008-02-27 03:19:58 +08:00
|
|
|
Start = End - 1;
|
2008-11-10 02:54:14 +08:00
|
|
|
} else if (EscapedChar == '[') {
|
|
|
|
std::string SymbolicName;
|
|
|
|
|
|
|
|
Start++;
|
|
|
|
|
|
|
|
while (*Start && *Start != ']') {
|
|
|
|
SymbolicName += *Start;
|
|
|
|
|
|
|
|
Start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Start) {
|
|
|
|
// FIXME: Should be caught by sema.
|
|
|
|
assert(0 && "Could not parse symbolic name");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(*Start == ']' && "Error parsing symbolic name");
|
|
|
|
|
|
|
|
int Index = -1;
|
|
|
|
|
|
|
|
// Check if this is an output operand.
|
|
|
|
for (unsigned i = 0; i < S.getNumOutputs(); i++) {
|
|
|
|
if (S.getOutputName(i) == SymbolicName) {
|
|
|
|
Index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Index == -1) {
|
|
|
|
for (unsigned i = 0; i < S.getNumInputs(); i++) {
|
|
|
|
if (S.getInputName(i) == SymbolicName) {
|
|
|
|
Index = S.getNumOutputs() + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Index != -1 && "Did not find right operand!");
|
|
|
|
|
|
|
|
Result += '$' + llvm::utostr(Index);
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
} else {
|
2008-10-18 04:58:01 +08:00
|
|
|
Failed = true;
|
|
|
|
return "";
|
2008-02-06 00:35:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-02-27 02:33:46 +08:00
|
|
|
static std::string SimplifyConstraint(const char* Constraint,
|
2009-01-18 10:06:20 +08:00
|
|
|
TargetInfo &Target,
|
|
|
|
const std::string *OutputNamesBegin = 0,
|
|
|
|
const std::string *OutputNamesEnd = 0)
|
|
|
|
{
|
2008-02-06 00:35:33 +08:00
|
|
|
std::string Result;
|
|
|
|
|
|
|
|
while (*Constraint) {
|
|
|
|
switch (*Constraint) {
|
|
|
|
default:
|
2008-02-27 02:33:46 +08:00
|
|
|
Result += Target.convertConstraint(*Constraint);
|
2008-02-06 00:35:33 +08:00
|
|
|
break;
|
|
|
|
// Ignore these
|
|
|
|
case '*':
|
|
|
|
case '?':
|
|
|
|
case '!':
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
Result += "imr";
|
|
|
|
break;
|
2009-01-18 10:06:20 +08:00
|
|
|
case '[': {
|
|
|
|
assert(OutputNamesBegin && OutputNamesEnd &&
|
|
|
|
"Must pass output names to constraints with a symbolic name");
|
|
|
|
unsigned Index;
|
|
|
|
bool result = Target.resolveSymbolicName(Constraint,
|
|
|
|
OutputNamesBegin,
|
|
|
|
OutputNamesEnd, Index);
|
2009-01-21 15:35:26 +08:00
|
|
|
assert(result && "Could not resolve symbolic name"); result=result;
|
2009-01-18 10:06:20 +08:00
|
|
|
Result += llvm::utostr(Index);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-06 00:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Constraint++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-01-12 03:32:54 +08:00
|
|
|
llvm::Value* CodeGenFunction::EmitAsmInput(const AsmStmt &S,
|
|
|
|
TargetInfo::ConstraintInfo Info,
|
|
|
|
const Expr *InputExpr,
|
|
|
|
std::string &ConstraintStr)
|
|
|
|
{
|
|
|
|
llvm::Value *Arg;
|
|
|
|
if ((Info & TargetInfo::CI_AllowsRegister) ||
|
2009-01-12 10:22:13 +08:00
|
|
|
!(Info & TargetInfo::CI_AllowsMemory)) {
|
|
|
|
const llvm::Type *Ty = ConvertType(InputExpr->getType());
|
|
|
|
|
|
|
|
if (Ty->isSingleValueType()) {
|
2009-01-12 03:32:54 +08:00
|
|
|
Arg = EmitScalarExpr(InputExpr);
|
|
|
|
} else {
|
2009-01-12 10:22:13 +08:00
|
|
|
LValue Dest = EmitLValue(InputExpr);
|
|
|
|
|
|
|
|
uint64_t Size = CGM.getTargetData().getTypeSizeInBits(Ty);
|
|
|
|
if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
|
|
|
|
Ty = llvm::IntegerType::get(Size);
|
|
|
|
Ty = llvm::PointerType::getUnqual(Ty);
|
|
|
|
|
|
|
|
Arg = Builder.CreateLoad(Builder.CreateBitCast(Dest.getAddress(), Ty));
|
|
|
|
} else {
|
|
|
|
Arg = Dest.getAddress();
|
|
|
|
ConstraintStr += '*';
|
|
|
|
}
|
2009-01-12 03:32:54 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LValue Dest = EmitLValue(InputExpr);
|
|
|
|
Arg = Dest.getAddress();
|
|
|
|
ConstraintStr += '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
return Arg;
|
|
|
|
}
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
|
2008-10-18 04:58:01 +08:00
|
|
|
bool Failed;
|
2008-02-06 00:35:33 +08:00
|
|
|
std::string AsmString =
|
2008-11-10 02:54:14 +08:00
|
|
|
ConvertAsmString(S, Failed);
|
2008-10-18 04:58:01 +08:00
|
|
|
|
|
|
|
if (Failed) {
|
|
|
|
ErrorUnsupported(&S, "asm string");
|
|
|
|
return;
|
|
|
|
}
|
2008-02-06 00:35:33 +08:00
|
|
|
|
|
|
|
std::string Constraints;
|
|
|
|
|
|
|
|
llvm::Value *ResultAddr = 0;
|
|
|
|
const llvm::Type *ResultType = llvm::Type::VoidTy;
|
|
|
|
|
|
|
|
std::vector<const llvm::Type*> ArgTypes;
|
|
|
|
std::vector<llvm::Value*> Args;
|
2008-02-06 04:01:53 +08:00
|
|
|
|
|
|
|
// Keep track of inout constraints.
|
|
|
|
std::string InOutConstraints;
|
|
|
|
std::vector<llvm::Value*> InOutArgs;
|
|
|
|
std::vector<const llvm::Type*> InOutArgTypes;
|
2009-01-28 04:38:24 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
|
|
|
|
std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(),
|
|
|
|
S.getOutputConstraint(i)->getByteLength());
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo Info;
|
|
|
|
bool result = Target.validateOutputConstraint(OutputConstraint.c_str(),
|
|
|
|
Info);
|
2008-10-12 08:31:50 +08:00
|
|
|
assert(result && "Failed to parse output constraint"); result=result;
|
2008-02-06 00:35:33 +08:00
|
|
|
|
2009-01-28 04:38:24 +08:00
|
|
|
OutputConstraintInfos.push_back(Info);
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
// Simplify the output constraint.
|
2008-02-27 02:33:46 +08:00
|
|
|
OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
|
2008-02-06 00:35:33 +08:00
|
|
|
|
|
|
|
LValue Dest = EmitLValue(S.getOutputExpr(i));
|
|
|
|
const llvm::Type *DestValueType =
|
|
|
|
cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType();
|
|
|
|
|
|
|
|
// If the first output operand is not a memory dest, we'll
|
|
|
|
// make it the return value.
|
|
|
|
if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) &&
|
2008-05-23 06:12:56 +08:00
|
|
|
DestValueType->isSingleValueType()) {
|
2008-02-06 00:35:33 +08:00
|
|
|
ResultAddr = Dest.getAddress();
|
|
|
|
ResultType = DestValueType;
|
|
|
|
Constraints += "=" + OutputConstraint;
|
|
|
|
} else {
|
|
|
|
ArgTypes.push_back(Dest.getAddress()->getType());
|
2008-02-06 00:57:38 +08:00
|
|
|
Args.push_back(Dest.getAddress());
|
2008-02-06 00:35:33 +08:00
|
|
|
if (i != 0)
|
|
|
|
Constraints += ',';
|
2008-02-06 04:01:53 +08:00
|
|
|
Constraints += "=*";
|
2008-02-06 00:35:33 +08:00
|
|
|
Constraints += OutputConstraint;
|
2008-02-06 04:01:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Info & TargetInfo::CI_ReadWrite) {
|
|
|
|
InOutConstraints += ',';
|
2009-01-12 03:32:54 +08:00
|
|
|
|
2008-02-06 04:01:53 +08:00
|
|
|
const Expr *InputExpr = S.getOutputExpr(i);
|
2009-01-12 03:32:54 +08:00
|
|
|
llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, InOutConstraints);
|
2008-02-06 04:01:53 +08:00
|
|
|
|
2009-01-12 05:23:27 +08:00
|
|
|
if (Info & TargetInfo::CI_AllowsRegister)
|
|
|
|
InOutConstraints += llvm::utostr(i);
|
|
|
|
else
|
|
|
|
InOutConstraints += OutputConstraint;
|
2009-01-12 03:46:50 +08:00
|
|
|
|
2008-02-06 04:01:53 +08:00
|
|
|
InOutArgTypes.push_back(Arg->getType());
|
|
|
|
InOutArgs.push_back(Arg);
|
|
|
|
}
|
2008-02-06 00:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
|
|
|
|
const Expr *InputExpr = S.getInputExpr(i);
|
|
|
|
|
|
|
|
std::string InputConstraint(S.getInputConstraint(i)->getStrData(),
|
|
|
|
S.getInputConstraint(i)->getByteLength());
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo Info;
|
|
|
|
bool result = Target.validateInputConstraint(InputConstraint.c_str(),
|
2009-01-18 07:36:15 +08:00
|
|
|
S.begin_output_names(),
|
|
|
|
S.end_output_names(),
|
2009-01-28 04:38:24 +08:00
|
|
|
&OutputConstraintInfos[0],
|
2009-01-21 15:35:26 +08:00
|
|
|
Info); result=result;
|
2009-01-18 09:56:57 +08:00
|
|
|
assert(result && "Failed to parse input constraint");
|
2008-02-06 00:35:33 +08:00
|
|
|
|
|
|
|
if (i != 0 || S.getNumOutputs() > 0)
|
|
|
|
Constraints += ',';
|
|
|
|
|
|
|
|
// Simplify the input constraint.
|
2009-01-18 10:06:20 +08:00
|
|
|
InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target,
|
|
|
|
S.begin_output_names(),
|
|
|
|
S.end_output_names());
|
2008-02-06 00:35:33 +08:00
|
|
|
|
2009-01-12 03:32:54 +08:00
|
|
|
llvm::Value *Arg = EmitAsmInput(S, Info, InputExpr, Constraints);
|
2008-02-06 00:35:33 +08:00
|
|
|
|
|
|
|
ArgTypes.push_back(Arg->getType());
|
|
|
|
Args.push_back(Arg);
|
|
|
|
Constraints += InputConstraint;
|
|
|
|
}
|
|
|
|
|
2008-02-06 04:01:53 +08:00
|
|
|
// Append the "input" part of inout constraints last.
|
|
|
|
for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
|
|
|
|
ArgTypes.push_back(InOutArgTypes[i]);
|
|
|
|
Args.push_back(InOutArgs[i]);
|
|
|
|
}
|
|
|
|
Constraints += InOutConstraints;
|
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
// Clobbers
|
|
|
|
for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
|
|
|
|
std::string Clobber(S.getClobber(i)->getStrData(),
|
|
|
|
S.getClobber(i)->getByteLength());
|
|
|
|
|
|
|
|
Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str());
|
|
|
|
|
2008-02-06 08:11:32 +08:00
|
|
|
if (i != 0 || NumConstraints != 0)
|
2008-02-06 00:35:33 +08:00
|
|
|
Constraints += ',';
|
2008-02-06 08:11:32 +08:00
|
|
|
|
|
|
|
Constraints += "~{";
|
2008-02-06 00:35:33 +08:00
|
|
|
Constraints += Clobber;
|
2008-02-06 08:11:32 +08:00
|
|
|
Constraints += '}';
|
2008-02-06 00:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add machine specific clobbers
|
2008-12-21 09:15:32 +08:00
|
|
|
std::string MachineClobbers = Target.getClobbers();
|
|
|
|
if (!MachineClobbers.empty()) {
|
2008-02-06 00:35:33 +08:00
|
|
|
if (!Constraints.empty())
|
|
|
|
Constraints += ',';
|
2008-12-21 09:15:32 +08:00
|
|
|
Constraints += MachineClobbers;
|
2008-02-06 00:35:33 +08:00
|
|
|
}
|
2008-02-06 04:01:53 +08:00
|
|
|
|
2008-02-06 00:35:33 +08:00
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(ResultType, ArgTypes, false);
|
|
|
|
|
|
|
|
llvm::InlineAsm *IA =
|
|
|
|
llvm::InlineAsm::get(FTy, AsmString, Constraints,
|
|
|
|
S.isVolatile() || S.getNumOutputs() == 0);
|
|
|
|
llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), "");
|
2008-06-14 07:01:12 +08:00
|
|
|
if (ResultAddr) // FIXME: volatility
|
2008-02-06 00:35:33 +08:00
|
|
|
Builder.CreateStore(Result, ResultAddr);
|
|
|
|
}
|