blob: 909dab9a65167be83c5340ee48648e9aaa3914ca [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000092 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000095 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096}
97
Gordon Henriksen14a55692007-12-10 02:14:30 +000098PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +000099 if (OperandList)
100 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101}
102
103// removeIncomingValue - Remove an incoming value. This is useful if a
104// predecessor basic block is deleted.
105Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
106 unsigned NumOps = getNumOperands();
107 Use *OL = OperandList;
108 assert(Idx*2 < NumOps && "BB not in PHI node!");
109 Value *Removed = OL[Idx*2];
110
111 // Move everything after this operand down.
112 //
113 // FIXME: we could just swap with the end of the list, then erase. However,
114 // client might not expect this to happen. The code as it is thrashes the
115 // use/def lists, which is kinda lame.
116 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
117 OL[i-2] = OL[i];
118 OL[i-2+1] = OL[i+1];
119 }
120
121 // Nuke the last value.
122 OL[NumOps-2].set(0);
123 OL[NumOps-2+1].set(0);
124 NumOperands = NumOps-2;
125
126 // If the PHI node is dead, because it has zero entries, nuke it now.
127 if (NumOps == 2 && DeletePHIIfEmpty) {
128 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000129 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 eraseFromParent();
131 }
132 return Removed;
133}
134
135/// resizeOperands - resize operands - This adjusts the length of the operands
136/// list according to the following behavior:
137/// 1. If NumOps == 0, grow the operand list in response to a push_back style
138/// of operation. This grows the number of ops by 1.5 times.
139/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
140/// 3. If NumOps == NumOperands, trim the reserved space.
141///
142void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
147 } else if (NumOps*2 > NumOperands) {
148 // No resize needed.
149 if (ReservedSpace >= NumOps) return;
150 } else if (NumOps == NumOperands) {
151 if (ReservedSpace == NumOps) return;
152 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154 }
155
156 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000158 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000159 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000161 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Nate Begemanb3923212005-08-04 23:24:19 +0000164/// hasConstantValue - If the specified PHI node always merges together the same
165/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000166Value *PHINode::hasConstantValue() const {
167 // Exploit the fact that phi nodes always have at least one entry.
168 Value *ConstantValue = getIncomingValue(0);
169 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
170 if (getIncomingValue(i) != ConstantValue)
171 return 0; // Incoming values not all the same.
172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175
176//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000177// CallInst Implementation
178//===----------------------------------------------------------------------===//
179
Gordon Henriksen14a55692007-12-10 02:14:30 +0000180CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181}
182
Chris Lattner054ba2c2007-02-13 00:58:44 +0000183void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000184 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000185 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000186
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000188 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000189 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000190
Chris Lattner054ba2c2007-02-13 00:58:44 +0000191 assert((NumParams == FTy->getNumParams() ||
192 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000193 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000195 assert((i >= FTy->getNumParams() ||
196 FTy->getParamType(i) == Params[i]->getType()) &&
197 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000198 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000199 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000200}
201
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000204 Op<-1>() = Func;
205 Op<0>() = Actual1;
206 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207
208 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000210 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000213 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000215 assert((0 >= FTy->getNumParams() ||
216 FTy->getParamType(0) == Actual1->getType()) &&
217 "Calling a function with a bad signature!");
218 assert((1 >= FTy->getNumParams() ||
219 FTy->getParamType(1) == Actual2->getType()) &&
220 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221}
222
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000225 Op<-1>() = Func;
226 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000227
228 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000230 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232 assert((FTy->getNumParams() == 1 ||
233 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000235 assert((0 == FTy->getNumParams() ||
236 FTy->getParamType(0) == Actual->getType()) &&
237 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238}
239
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000241 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000242 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000243
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000244 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000246 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000248 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249}
250
Daniel Dunbar4975db62009-07-25 04:41:11 +0000251CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000252 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
254 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 Instruction::Call,
256 OperandTraits<CallInst>::op_end(this) - 2,
257 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000259 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260}
261
Daniel Dunbar4975db62009-07-25 04:41:11 +0000262CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 BasicBlock *InsertAtEnd)
264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000266 Instruction::Call,
267 OperandTraits<CallInst>::op_end(this) - 2,
268 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000270 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000272CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 Instruction *InsertBefore)
274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
275 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000276 Instruction::Call,
277 OperandTraits<CallInst>::op_end(this) - 1,
278 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000280 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281}
282
Daniel Dunbar4975db62009-07-25 04:41:11 +0000283CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 BasicBlock *InsertAtEnd)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000287 Instruction::Call,
288 OperandTraits<CallInst>::op_end(this) - 1,
289 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000291 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000295 : Instruction(CI.getType(), Instruction::Call,
296 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000297 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000298 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000299 setTailCall(CI.isTailCall());
300 setCallingConv(CI.getCallingConv());
301
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 Use *OL = OperandList;
303 Use *InOL = CI.OperandList;
304 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000306 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Devang Patel4c758ea2008-09-25 21:00:45 +0000309void CallInst::addAttribute(unsigned i, Attributes attr) {
310 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000311 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000312 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000313}
314
Devang Patel4c758ea2008-09-25 21:00:45 +0000315void CallInst::removeAttribute(unsigned i, Attributes attr) {
316 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000317 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000319}
320
Devang Patelba3fa6c2008-09-23 23:03:40 +0000321bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000322 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000323 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000324 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000325 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000326 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000327}
328
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000329/// IsConstantOne - Return true only if val is constant int 1
330static bool IsConstantOne(Value *val) {
331 assert(val && "IsConstantOne does not work with NULL val");
332 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
333}
334
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000335static Instruction *createMalloc(Instruction *InsertBefore,
336 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000337 const Type *AllocTy, Value *AllocSize,
338 Value *ArraySize, Function *MallocF,
339 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000340 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000341 "createMalloc needs either InsertBefore or InsertAtEnd");
342
343 // malloc(type) becomes:
344 // bitcast (i8* malloc(typeSize)) to type*
345 // malloc(type, arraySize) becomes:
346 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000347 if (!ArraySize)
348 ArraySize = ConstantInt::get(IntPtrTy, 1);
349 else if (ArraySize->getType() != IntPtrTy) {
350 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000351 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
352 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000353 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000354 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
355 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000356 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000357
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000358 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000359 if (IsConstantOne(AllocSize)) {
360 AllocSize = ArraySize; // Operand * 1 = Operand
361 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
362 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
363 false /*ZExt*/);
364 // Malloc arg is constant product of type size and array size
365 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
366 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000367 // Multiply type size by the array size...
368 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000369 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
370 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000372 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
373 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000374 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000375 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376
Victor Hernandez788eaab2009-09-18 19:20:02 +0000377 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 // Create the call to Malloc.
379 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
380 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000381 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000382 Value *MallocFunc = MallocF;
383 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000384 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000385 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000386 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000388 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000390 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000391 Result = MCall;
392 if (Result->getType() != AllocPtrType)
393 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000394 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000395 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000396 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000397 Result = MCall;
398 if (Result->getType() != AllocPtrType) {
399 InsertAtEnd->getInstList().push_back(MCall);
400 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000401 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000403 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000405 if (Function *F = dyn_cast<Function>(MallocFunc)) {
406 MCall->setCallingConv(F->getCallingConv());
407 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
408 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000409 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000411 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412}
413
414/// CreateMalloc - Generate the IR for a call to malloc:
415/// 1. Compute the malloc call's argument as the specified type's size,
416/// possibly multiplied by the array size if the array size is not
417/// constant 1.
418/// 2. Call malloc with that argument.
419/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000420Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
421 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000422 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000423 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 const Twine &Name) {
425 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000426 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427}
428
429/// CreateMalloc - Generate the IR for a call to malloc:
430/// 1. Compute the malloc call's argument as the specified type's size,
431/// possibly multiplied by the array size if the array size is not
432/// constant 1.
433/// 2. Call malloc with that argument.
434/// 3. Bitcast the result of the malloc call to the specified type.
435/// Note: This function does not add the bitcast to the basic block, that is the
436/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000437Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
438 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000439 Value *AllocSize, Value *ArraySize,
440 Function *MallocF, const Twine &Name) {
441 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000444
Victor Hernandeze2971492009-10-24 04:23:03 +0000445static Instruction* createFree(Value* Source, Instruction *InsertBefore,
446 BasicBlock *InsertAtEnd) {
447 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
448 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000449 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000450 "Can not free something of nonpointer type!");
451
452 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
453 Module* M = BB->getParent()->getParent();
454
455 const Type *VoidTy = Type::getVoidTy(M->getContext());
456 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
457 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000458 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000459 CallInst* Result = NULL;
460 Value *PtrCast = Source;
461 if (InsertBefore) {
462 if (Source->getType() != IntPtrTy)
463 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
464 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
465 } else {
466 if (Source->getType() != IntPtrTy)
467 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
468 Result = CallInst::Create(FreeFunc, PtrCast, "");
469 }
470 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000471 if (Function *F = dyn_cast<Function>(FreeFunc))
472 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000473
474 return Result;
475}
476
477/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000478Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
479 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000480}
481
482/// CreateFree - Generate the IR for a call to the builtin free function.
483/// Note: This function does not add the call to the basic block, that is the
484/// responsibility of the caller.
485Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
486 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
487 assert(FreeCall && "CreateFree did not create a CallInst");
488 return FreeCall;
489}
490
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491//===----------------------------------------------------------------------===//
492// InvokeInst Implementation
493//===----------------------------------------------------------------------===//
494
495void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000496 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000497 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000498 Op<-3>() = Fn;
499 Op<-2>() = IfNormal;
500 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000501 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000503 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000505 assert(((NumArgs == FTy->getNumParams()) ||
506 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000507 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000509 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000510 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000511 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000512 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000513 "Invoking a function with a bad signature!");
514
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000515 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000516 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000517}
518
Misha Brukmanb1c93172005-04-21 23:48:37 +0000519InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000520 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000521 OperandTraits<InvokeInst>::op_end(this)
522 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000523 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000524 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000525 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000526 Use *OL = OperandList, *InOL = II.OperandList;
527 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000528 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000529 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
533 return getSuccessor(idx);
534}
535unsigned InvokeInst::getNumSuccessorsV() const {
536 return getNumSuccessors();
537}
538void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
539 return setSuccessor(idx, B);
540}
541
Devang Patelba3fa6c2008-09-23 23:03:40 +0000542bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000544 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000545 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000546 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000547 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000548}
549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550void InvokeInst::addAttribute(unsigned i, Attributes attr) {
551 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000552 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000553 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000554}
555
Devang Patel4c758ea2008-09-25 21:00:45 +0000556void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
557 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000558 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000560}
561
Duncan Sands5208d1a2007-11-28 17:07:01 +0000562
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563//===----------------------------------------------------------------------===//
564// ReturnInst Implementation
565//===----------------------------------------------------------------------===//
566
Chris Lattner2195fc42007-02-24 00:55:48 +0000567ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000568 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000569 OperandTraits<ReturnInst>::op_end(this) -
570 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000571 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000572 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000573 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000574 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000575}
576
Owen Anderson55f1c092009-08-13 21:58:54 +0000577ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
578 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000579 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
580 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000581 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000582 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000583}
Owen Anderson55f1c092009-08-13 21:58:54 +0000584ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000586 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
587 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000588 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000589 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000590}
Owen Anderson55f1c092009-08-13 21:58:54 +0000591ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
592 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000593 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000594}
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596unsigned ReturnInst::getNumSuccessorsV() const {
597 return getNumSuccessors();
598}
599
Devang Patelae682fb2008-02-26 17:56:20 +0000600/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
601/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000603 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000607 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 return 0;
609}
610
Devang Patel59643e52008-02-23 00:35:18 +0000611ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000612}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614//===----------------------------------------------------------------------===//
615// UnwindInst Implementation
616//===----------------------------------------------------------------------===//
617
Owen Anderson55f1c092009-08-13 21:58:54 +0000618UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
619 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
620 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000621}
Owen Anderson55f1c092009-08-13 21:58:54 +0000622UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
623 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
624 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
626
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628unsigned UnwindInst::getNumSuccessorsV() const {
629 return getNumSuccessors();
630}
631
632void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000633 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634}
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000637 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 return 0;
639}
640
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000642// UnreachableInst Implementation
643//===----------------------------------------------------------------------===//
644
Owen Anderson55f1c092009-08-13 21:58:54 +0000645UnreachableInst::UnreachableInst(LLVMContext &Context,
646 Instruction *InsertBefore)
647 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
648 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000649}
Owen Anderson55f1c092009-08-13 21:58:54 +0000650UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
652 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655unsigned UnreachableInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
657}
658
659void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000660 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661}
662
663BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000664 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000666}
667
668//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000669// BranchInst Implementation
670//===----------------------------------------------------------------------===//
671
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672void BranchInst::AssertOK() {
673 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000674 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676}
677
Chris Lattner2195fc42007-02-24 00:55:48 +0000678BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000679 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000680 OperandTraits<BranchInst>::op_end(this) - 1,
681 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000682 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000683 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000684}
685BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
686 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000688 OperandTraits<BranchInst>::op_end(this) - 3,
689 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000690 Op<-1>() = IfTrue;
691 Op<-2>() = IfFalse;
692 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000693#ifndef NDEBUG
694 AssertOK();
695#endif
696}
697
698BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000699 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000700 OperandTraits<BranchInst>::op_end(this) - 1,
701 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000702 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000703 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
706BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
707 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000708 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 OperandTraits<BranchInst>::op_end(this) - 3,
710 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000711 Op<-1>() = IfTrue;
712 Op<-2>() = IfFalse;
713 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000714#ifndef NDEBUG
715 AssertOK();
716#endif
717}
718
719
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000721 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000722 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
723 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000724 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 if (BI.getNumOperands() != 1) {
726 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000727 Op<-3>() = BI.Op<-3>();
728 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000730 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
734 return getSuccessor(idx);
735}
736unsigned BranchInst::getNumSuccessorsV() const {
737 return getNumSuccessors();
738}
739void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
740 setSuccessor(idx, B);
741}
742
743
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000744//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000745// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746//===----------------------------------------------------------------------===//
747
Owen Andersonb6b25302009-07-14 23:09:55 +0000748static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000751 else {
752 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000753 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000754 assert(Amt->getType()->isIntegerTy() &&
755 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000756 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000757 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000758}
759
Victor Hernandez8acf2952009-10-23 21:09:37 +0000760AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
761 const Twine &Name, Instruction *InsertBefore)
762 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
763 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
764 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000765 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000766 setName(Name);
767}
768
769AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
770 const Twine &Name, BasicBlock *InsertAtEnd)
771 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
772 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
773 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000774 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000775 setName(Name);
776}
777
778AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
779 Instruction *InsertBefore)
780 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
781 getAISize(Ty->getContext(), 0), InsertBefore) {
782 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000783 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000784 setName(Name);
785}
786
787AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
788 BasicBlock *InsertAtEnd)
789 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
790 getAISize(Ty->getContext(), 0), InsertAtEnd) {
791 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000792 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000793 setName(Name);
794}
795
796AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
797 const Twine &Name, Instruction *InsertBefore)
798 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000799 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000800 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000801 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000802 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
Victor Hernandez8acf2952009-10-23 21:09:37 +0000805AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
806 const Twine &Name, BasicBlock *InsertAtEnd)
807 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000808 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000809 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000810 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000811 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
Gordon Henriksen14a55692007-12-10 02:14:30 +0000814// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000815AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000816}
817
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000819 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000820 assert(Align <= MaximumAlignment &&
821 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000822 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000823 assert(getAlignment() == Align && "Alignment representation error!");
824}
825
Victor Hernandez8acf2952009-10-23 21:09:37 +0000826bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000827 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000828 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830}
831
Victor Hernandez8acf2952009-10-23 21:09:37 +0000832const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000833 return getType()->getElementType();
834}
835
Chris Lattner8b291e62008-11-26 02:54:17 +0000836/// isStaticAlloca - Return true if this alloca is in the entry block of the
837/// function and is a constant size. If so, the code generator will fold it
838/// into the prolog/epilog code, so it is basically free.
839bool AllocaInst::isStaticAlloca() const {
840 // Must be constant size.
841 if (!isa<ConstantInt>(getArraySize())) return false;
842
843 // Must be in the entry block.
844 const BasicBlock *Parent = getParent();
845 return Parent == &Parent->getParent()->front();
846}
847
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000848//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849// LoadInst Implementation
850//===----------------------------------------------------------------------===//
851
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000853 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855}
856
Daniel Dunbar4975db62009-07-25 04:41:11 +0000857LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000860 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000861 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000862 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000863 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864}
865
Daniel Dunbar4975db62009-07-25 04:41:11 +0000866LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000869 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873}
874
Daniel Dunbar4975db62009-07-25 04:41:11 +0000875LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000876 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000878 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000879 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000880 setAlignment(0);
881 AssertOK();
882 setName(Name);
883}
884
Daniel Dunbar4975db62009-07-25 04:41:11 +0000885LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000886 unsigned Align, Instruction *InsertBef)
887 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
888 Load, Ptr, InsertBef) {
889 setVolatile(isVolatile);
890 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000892 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893}
894
Daniel Dunbar4975db62009-07-25 04:41:11 +0000895LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000896 unsigned Align, BasicBlock *InsertAE)
897 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
898 Load, Ptr, InsertAE) {
899 setVolatile(isVolatile);
900 setAlignment(Align);
901 AssertOK();
902 setName(Name);
903}
904
Daniel Dunbar4975db62009-07-25 04:41:11 +0000905LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000908 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000909 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000910 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000911 AssertOK();
912 setName(Name);
913}
914
Daniel Dunbar27096822009-08-11 18:11:15 +0000915
916
917LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
918 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
919 Load, Ptr, InsertBef) {
920 setVolatile(false);
921 setAlignment(0);
922 AssertOK();
923 if (Name && Name[0]) setName(Name);
924}
925
926LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
928 Load, Ptr, InsertAE) {
929 setVolatile(false);
930 setAlignment(0);
931 AssertOK();
932 if (Name && Name[0]) setName(Name);
933}
934
935LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
936 Instruction *InsertBef)
937: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
938 Load, Ptr, InsertBef) {
939 setVolatile(isVolatile);
940 setAlignment(0);
941 AssertOK();
942 if (Name && Name[0]) setName(Name);
943}
944
945LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
946 BasicBlock *InsertAE)
947 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
948 Load, Ptr, InsertAE) {
949 setVolatile(isVolatile);
950 setAlignment(0);
951 AssertOK();
952 if (Name && Name[0]) setName(Name);
953}
954
Christopher Lamb84485702007-04-22 19:24:39 +0000955void LoadInst::setAlignment(unsigned Align) {
956 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000957 assert(Align <= MaximumAlignment &&
958 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000959 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
960 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000961 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000962}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963
964//===----------------------------------------------------------------------===//
965// StoreInst Implementation
966//===----------------------------------------------------------------------===//
967
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000969 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000970 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000971 "Ptr must have pointer type!");
972 assert(getOperand(0)->getType() ==
973 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000974 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000975}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977
978StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000979 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000980 OperandTraits<StoreInst>::op_begin(this),
981 OperandTraits<StoreInst>::operands(this),
982 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000983 Op<0>() = val;
984 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000985 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000986 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 AssertOK();
988}
989
990StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000991 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000992 OperandTraits<StoreInst>::op_begin(this),
993 OperandTraits<StoreInst>::operands(this),
994 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000995 Op<0>() = val;
996 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000997 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000998 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 AssertOK();
1000}
1001
Misha Brukmanb1c93172005-04-21 23:48:37 +00001002StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001004 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 OperandTraits<StoreInst>::op_begin(this),
1006 OperandTraits<StoreInst>::operands(this),
1007 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001008 Op<0>() = val;
1009 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001010 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setAlignment(0);
1012 AssertOK();
1013}
1014
1015StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1016 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001017 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001018 OperandTraits<StoreInst>::op_begin(this),
1019 OperandTraits<StoreInst>::operands(this),
1020 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001021 Op<0>() = val;
1022 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001023 setVolatile(isVolatile);
1024 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001025 AssertOK();
1026}
1027
Misha Brukmanb1c93172005-04-21 23:48:37 +00001028StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001029 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001030 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001031 OperandTraits<StoreInst>::op_begin(this),
1032 OperandTraits<StoreInst>::operands(this),
1033 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001034 Op<0>() = val;
1035 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001036 setVolatile(isVolatile);
1037 setAlignment(Align);
1038 AssertOK();
1039}
1040
1041StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001043 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001044 OperandTraits<StoreInst>::op_begin(this),
1045 OperandTraits<StoreInst>::operands(this),
1046 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001047 Op<0>() = val;
1048 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001049 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001050 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001051 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001052}
1053
Christopher Lamb84485702007-04-22 19:24:39 +00001054void StoreInst::setAlignment(unsigned Align) {
1055 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001056 assert(Align <= MaximumAlignment &&
1057 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001058 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1059 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001060 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001061}
1062
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063//===----------------------------------------------------------------------===//
1064// GetElementPtrInst Implementation
1065//===----------------------------------------------------------------------===//
1066
Christopher Lambedf07882007-12-17 01:12:55 +00001067static unsigned retrieveAddrSpace(const Value *Val) {
1068 return cast<PointerType>(Val->getType())->getAddressSpace();
1069}
1070
Gabor Greif21ba1842008-06-06 20:28:12 +00001071void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001072 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001073 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1074 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001075 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001076
Chris Lattner79807c3d2007-01-31 19:47:18 +00001077 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001078 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001079
1080 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081}
1082
Daniel Dunbar4975db62009-07-25 04:41:11 +00001083void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001084 assert(NumOperands == 2 && "NumOperands not initialized?");
1085 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001086 OL[0] = Ptr;
1087 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001088
1089 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001090}
1091
Gabor Greiff6caff662008-05-10 08:32:32 +00001092GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001093 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001094 OperandTraits<GetElementPtrInst>::op_end(this)
1095 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001096 GEPI.getNumOperands()) {
1097 Use *OL = OperandList;
1098 Use *GEPIOL = GEPI.OperandList;
1099 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001100 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001101 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001102}
1103
Chris Lattner82981202005-05-03 05:43:30 +00001104GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001105 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001106 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001107 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001108 GetElementPtr,
1109 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1110 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001111 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001112}
1113
1114GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001115 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001116 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001117 checkType(getIndexedType(Ptr->getType(),Idx)),
1118 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001119 GetElementPtr,
1120 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1121 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001122 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001123}
1124
Chris Lattner6090a422009-03-09 04:46:40 +00001125/// getIndexedType - Returns the type of the element that would be accessed with
1126/// a gep instruction with the specified parameters.
1127///
1128/// The Idxs pointer should point to a continuous piece of memory containing the
1129/// indices, either as Value* or uint64_t.
1130///
1131/// A null type is returned if the indices are invalid for the specified
1132/// pointer type.
1133///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001134template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001135static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1136 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001137 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1138 if (!PTy) return 0; // Type isn't a pointer type!
1139 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001140
Chris Lattner6090a422009-03-09 04:46:40 +00001141 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001142 if (NumIdx == 0)
1143 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001144
1145 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001146 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1147 // that contain opaque types) under the assumption that it will be resolved to
1148 // a sane type later.
1149 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001150 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001151
Dan Gohman1ecaf452008-05-31 00:58:22 +00001152 unsigned CurIdx = 1;
1153 for (; CurIdx != NumIdx; ++CurIdx) {
1154 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001155 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001156 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001157 if (!CT->indexValid(Index)) return 0;
1158 Agg = CT->getTypeAtIndex(Index);
1159
1160 // If the new type forwards to another type, then it is in the middle
1161 // of being refined to another type (and hence, may have dropped all
1162 // references to what it was using before). So, use the new forwarded
1163 // type.
1164 if (const Type *Ty = Agg->getForwardedType())
1165 Agg = Ty;
1166 }
1167 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001168}
1169
Matthijs Kooijman04468622008-07-29 08:46:11 +00001170const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1171 Value* const *Idxs,
1172 unsigned NumIdx) {
1173 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1174}
1175
1176const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001177 Constant* const *Idxs,
1178 unsigned NumIdx) {
1179 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1180}
1181
1182const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Matthijs Kooijman04468622008-07-29 08:46:11 +00001183 uint64_t const *Idxs,
1184 unsigned NumIdx) {
1185 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1186}
1187
Chris Lattner82981202005-05-03 05:43:30 +00001188const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1189 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1190 if (!PTy) return 0; // Type isn't a pointer type!
1191
1192 // Check the pointer index.
1193 if (!PTy->indexValid(Idx)) return 0;
1194
Chris Lattnerc2233332005-05-03 16:44:45 +00001195 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001196}
1197
Chris Lattner45f15572007-04-14 00:12:57 +00001198
1199/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1200/// zeros. If so, the result pointer and the first operand have the same
1201/// value, just potentially different types.
1202bool GetElementPtrInst::hasAllZeroIndices() const {
1203 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1204 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1205 if (!CI->isZero()) return false;
1206 } else {
1207 return false;
1208 }
1209 }
1210 return true;
1211}
1212
Chris Lattner27058292007-04-27 20:35:56 +00001213/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1214/// constant integers. If so, the result pointer and the first operand have
1215/// a constant offset between them.
1216bool GetElementPtrInst::hasAllConstantIndices() const {
1217 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1218 if (!isa<ConstantInt>(getOperand(i)))
1219 return false;
1220 }
1221 return true;
1222}
1223
Dan Gohman1b849082009-09-07 23:54:19 +00001224void GetElementPtrInst::setIsInBounds(bool B) {
1225 cast<GEPOperator>(this)->setIsInBounds(B);
1226}
Chris Lattner45f15572007-04-14 00:12:57 +00001227
Nick Lewycky28a5f252009-09-27 21:33:04 +00001228bool GetElementPtrInst::isInBounds() const {
1229 return cast<GEPOperator>(this)->isInBounds();
1230}
1231
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001232//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001233// ExtractElementInst Implementation
1234//===----------------------------------------------------------------------===//
1235
1236ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001237 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001238 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001239 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001240 ExtractElement,
1241 OperandTraits<ExtractElementInst>::op_begin(this),
1242 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001243 assert(isValidOperands(Val, Index) &&
1244 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001245 Op<0>() = Val;
1246 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001247 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001248}
1249
1250ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001251 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001252 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001253 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001254 ExtractElement,
1255 OperandTraits<ExtractElementInst>::op_begin(this),
1256 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001257 assert(isValidOperands(Val, Index) &&
1258 "Invalid extractelement instruction operands!");
1259
Gabor Greif2d3024d2008-05-26 21:33:52 +00001260 Op<0>() = Val;
1261 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001262 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001263}
1264
Chris Lattner65511ff2006-10-05 06:24:58 +00001265
Chris Lattner54865b32006-04-08 04:05:48 +00001266bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001267 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001268 return false;
1269 return true;
1270}
1271
1272
Robert Bocchino23004482006-01-10 19:05:34 +00001273//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001274// InsertElementInst Implementation
1275//===----------------------------------------------------------------------===//
1276
Chris Lattner54865b32006-04-08 04:05:48 +00001277InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001278 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001279 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001280 : Instruction(Vec->getType(), InsertElement,
1281 OperandTraits<InsertElementInst>::op_begin(this),
1282 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001283 assert(isValidOperands(Vec, Elt, Index) &&
1284 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001285 Op<0>() = Vec;
1286 Op<1>() = Elt;
1287 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001288 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001289}
1290
Chris Lattner54865b32006-04-08 04:05:48 +00001291InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001292 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001293 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001294 : Instruction(Vec->getType(), InsertElement,
1295 OperandTraits<InsertElementInst>::op_begin(this),
1296 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001297 assert(isValidOperands(Vec, Elt, Index) &&
1298 "Invalid insertelement instruction operands!");
1299
Gabor Greif2d3024d2008-05-26 21:33:52 +00001300 Op<0>() = Vec;
1301 Op<1>() = Elt;
1302 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001303 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001304}
1305
Chris Lattner54865b32006-04-08 04:05:48 +00001306bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1307 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001308 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001309 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001310
Reid Spencerd84d35b2007-02-15 02:26:10 +00001311 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001312 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001313
Duncan Sands9dff9be2010-02-15 16:12:20 +00001314 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001315 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001316 return true;
1317}
1318
1319
Robert Bocchinoca27f032006-01-17 20:07:22 +00001320//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321// ShuffleVectorInst Implementation
1322//===----------------------------------------------------------------------===//
1323
1324ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001325 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001327: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001328 cast<VectorType>(Mask->getType())->getNumElements()),
1329 ShuffleVector,
1330 OperandTraits<ShuffleVectorInst>::op_begin(this),
1331 OperandTraits<ShuffleVectorInst>::operands(this),
1332 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001333 assert(isValidOperands(V1, V2, Mask) &&
1334 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001335 Op<0>() = V1;
1336 Op<1>() = V2;
1337 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001338 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001339}
1340
1341ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001342 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001343 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001344: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1345 cast<VectorType>(Mask->getType())->getNumElements()),
1346 ShuffleVector,
1347 OperandTraits<ShuffleVectorInst>::op_begin(this),
1348 OperandTraits<ShuffleVectorInst>::operands(this),
1349 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001350 assert(isValidOperands(V1, V2, Mask) &&
1351 "Invalid shuffle vector instruction operands!");
1352
Gabor Greif2d3024d2008-05-26 21:33:52 +00001353 Op<0>() = V1;
1354 Op<1>() = V2;
1355 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001356 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001357}
1358
Mon P Wang25f01062008-11-10 04:46:22 +00001359bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001360 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001361 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001362 return false;
1363
1364 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001365 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001366 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001367
1368 // Check to see if Mask is valid.
1369 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1370 const VectorType *VTy = cast<VectorType>(V1->getType());
1371 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1372 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1373 if (CI->uge(VTy->getNumElements()*2))
1374 return false;
1375 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1376 return false;
1377 }
1378 }
1379 }
1380 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1381 return false;
1382
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001383 return true;
1384}
1385
Chris Lattnerf724e342008-03-02 05:28:33 +00001386/// getMaskValue - Return the index from the shuffle mask for the specified
1387/// output result. This is either -1 if the element is undef or a number less
1388/// than 2*numelements.
1389int ShuffleVectorInst::getMaskValue(unsigned i) const {
1390 const Constant *Mask = cast<Constant>(getOperand(2));
1391 if (isa<UndefValue>(Mask)) return -1;
1392 if (isa<ConstantAggregateZero>(Mask)) return 0;
1393 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1394 assert(i < MaskCV->getNumOperands() && "Index out of range");
1395
1396 if (isa<UndefValue>(MaskCV->getOperand(i)))
1397 return -1;
1398 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1399}
1400
Dan Gohman12fce772008-05-15 19:50:34 +00001401//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001402// InsertValueInst Class
1403//===----------------------------------------------------------------------===//
1404
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001405void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001406 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001408 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1409 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001410 Op<0>() = Agg;
1411 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001412
Dan Gohmandd41bba2010-06-21 19:47:52 +00001413 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001414 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001415}
1416
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001417void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001418 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001419 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001420 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1421 && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 Op<0>() = Agg;
1423 Op<1>() = Val;
1424
1425 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001426 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001427}
1428
1429InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001430 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001431 OperandTraits<InsertValueInst>::op_begin(this), 2),
1432 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001433 Op<0>() = IVI.getOperand(0);
1434 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001435 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001436}
1437
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001438InsertValueInst::InsertValueInst(Value *Agg,
1439 Value *Val,
1440 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001441 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001442 Instruction *InsertBefore)
1443 : Instruction(Agg->getType(), InsertValue,
1444 OperandTraits<InsertValueInst>::op_begin(this),
1445 2, InsertBefore) {
1446 init(Agg, Val, Idx, Name);
1447}
1448
1449InsertValueInst::InsertValueInst(Value *Agg,
1450 Value *Val,
1451 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001452 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001453 BasicBlock *InsertAtEnd)
1454 : Instruction(Agg->getType(), InsertValue,
1455 OperandTraits<InsertValueInst>::op_begin(this),
1456 2, InsertAtEnd) {
1457 init(Agg, Val, Idx, Name);
1458}
1459
Dan Gohman0752bff2008-05-23 00:36:11 +00001460//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001461// ExtractValueInst Class
1462//===----------------------------------------------------------------------===//
1463
Gabor Greifcbcc4952008-06-06 21:06:32 +00001464void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001465 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001466 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001467
Dan Gohmandd41bba2010-06-21 19:47:52 +00001468 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001469 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001470}
1471
Daniel Dunbar4975db62009-07-25 04:41:11 +00001472void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001473 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001474
1475 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001476 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001477}
1478
1479ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001480 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001481 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001482 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001483}
1484
Dan Gohman12fce772008-05-15 19:50:34 +00001485// getIndexedType - Returns the type of the element that would be extracted
1486// with an extractvalue instruction with the specified parameters.
1487//
1488// A null type is returned if the indices are invalid for the specified
1489// pointer type.
1490//
1491const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001492 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001493 unsigned NumIdx) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001494 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001495 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001496 // We can't use CompositeType::indexValid(Index) here.
1497 // indexValid() always returns true for arrays because getelementptr allows
1498 // out-of-bounds indices. Since we don't allow those for extractvalue and
1499 // insertvalue we need to check array indexing manually.
1500 // Since the only other types we can index into are struct types it's just
1501 // as easy to check those manually as well.
1502 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1503 if (Index >= AT->getNumElements())
1504 return 0;
1505 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1506 if (Index >= ST->getNumElements())
1507 return 0;
1508 } else {
1509 // Not a valid type to index into.
1510 return 0;
1511 }
1512
1513 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001514
1515 // If the new type forwards to another type, then it is in the middle
1516 // of being refined to another type (and hence, may have dropped all
1517 // references to what it was using before). So, use the new forwarded
1518 // type.
1519 if (const Type *Ty = Agg->getForwardedType())
1520 Agg = Ty;
1521 }
Frits van Bommel16ebe772010-12-05 20:50:26 +00001522 return Agg;
Dan Gohman12fce772008-05-15 19:50:34 +00001523}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001524
Dan Gohman4a3125b2008-06-17 21:07:55 +00001525const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001526 unsigned Idx) {
1527 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001528}
1529
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001530//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001531// BinaryOperator Class
1532//===----------------------------------------------------------------------===//
1533
Chris Lattner2195fc42007-02-24 00:55:48 +00001534BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001535 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001536 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001537 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001538 OperandTraits<BinaryOperator>::op_begin(this),
1539 OperandTraits<BinaryOperator>::operands(this),
1540 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001541 Op<0>() = S1;
1542 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001543 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001544 setName(Name);
1545}
1546
1547BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001548 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001549 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001550 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001551 OperandTraits<BinaryOperator>::op_begin(this),
1552 OperandTraits<BinaryOperator>::operands(this),
1553 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001554 Op<0>() = S1;
1555 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001556 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001557 setName(Name);
1558}
1559
1560
1561void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001562 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001563 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001564 assert(LHS->getType() == RHS->getType() &&
1565 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001566#ifndef NDEBUG
1567 switch (iType) {
1568 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001569 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001570 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001571 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001572 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001573 "Tried to create an integer operation on a non-integer type!");
1574 break;
1575 case FAdd: case FSub:
1576 case FMul:
1577 assert(getType() == LHS->getType() &&
1578 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001579 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001580 "Tried to create a floating-point operation on a "
1581 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001582 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001583 case UDiv:
1584 case SDiv:
1585 assert(getType() == LHS->getType() &&
1586 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001587 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001588 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001589 "Incorrect operand type (not integer) for S/UDIV");
1590 break;
1591 case FDiv:
1592 assert(getType() == LHS->getType() &&
1593 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001594 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001595 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001596 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001597 case URem:
1598 case SRem:
1599 assert(getType() == LHS->getType() &&
1600 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001601 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001602 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001603 "Incorrect operand type (not integer) for S/UREM");
1604 break;
1605 case FRem:
1606 assert(getType() == LHS->getType() &&
1607 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001608 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001609 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001610 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001611 case Shl:
1612 case LShr:
1613 case AShr:
1614 assert(getType() == LHS->getType() &&
1615 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001616 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001617 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001618 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001619 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001620 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001621 case And: case Or:
1622 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001623 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001624 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001625 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001626 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001627 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001628 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001629 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001630 default:
1631 break;
1632 }
1633#endif
1634}
1635
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001636BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001637 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001638 Instruction *InsertBefore) {
1639 assert(S1->getType() == S2->getType() &&
1640 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001641 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001642}
1643
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001644BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001645 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001646 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001647 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 InsertAtEnd->getInstList().push_back(Res);
1649 return Res;
1650}
1651
Dan Gohman5476cfd2009-08-12 16:23:25 +00001652BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001653 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001654 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001655 return new BinaryOperator(Instruction::Sub,
1656 zero, Op,
1657 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001658}
1659
Dan Gohman5476cfd2009-08-12 16:23:25 +00001660BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001662 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001663 return new BinaryOperator(Instruction::Sub,
1664 zero, Op,
1665 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666}
1667
Dan Gohman4ab44202009-12-18 02:58:50 +00001668BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1669 Instruction *InsertBefore) {
1670 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1671 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1672}
1673
1674BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1675 BasicBlock *InsertAtEnd) {
1676 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1677 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1678}
1679
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001680BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1681 Instruction *InsertBefore) {
1682 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1683 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1684}
1685
1686BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1687 BasicBlock *InsertAtEnd) {
1688 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1689 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1690}
1691
Dan Gohman5476cfd2009-08-12 16:23:25 +00001692BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001693 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001694 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001695 return new BinaryOperator(Instruction::FSub,
1696 zero, Op,
1697 Op->getType(), Name, InsertBefore);
1698}
1699
Dan Gohman5476cfd2009-08-12 16:23:25 +00001700BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001701 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001702 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001703 return new BinaryOperator(Instruction::FSub,
1704 zero, Op,
1705 Op->getType(), Name, InsertAtEnd);
1706}
1707
Dan Gohman5476cfd2009-08-12 16:23:25 +00001708BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001710 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001711 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001712 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001713 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001714 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001715 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001716 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001717 }
1718
1719 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001720 Op->getType(), Name, InsertBefore);
1721}
1722
Dan Gohman5476cfd2009-08-12 16:23:25 +00001723BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001724 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001725 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001726 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001727 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001728 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001729 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001730 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001731 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001732 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001733 }
1734
1735 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001736 Op->getType(), Name, InsertAtEnd);
1737}
1738
1739
1740// isConstantAllOnes - Helper function for several functions below
1741static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001742 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1743 return CI->isAllOnesValue();
1744 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1745 return CV->isAllOnesValue();
1746 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001747}
1748
Owen Andersonbb2501b2009-07-13 22:18:28 +00001749bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001750 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1751 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001752 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1753 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 return false;
1755}
1756
Owen Andersonbb2501b2009-07-13 22:18:28 +00001757bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001758 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1759 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001760 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001761 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001762 return false;
1763}
1764
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001765bool BinaryOperator::isNot(const Value *V) {
1766 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1767 return (Bop->getOpcode() == Instruction::Xor &&
1768 (isConstantAllOnes(Bop->getOperand(1)) ||
1769 isConstantAllOnes(Bop->getOperand(0))));
1770 return false;
1771}
1772
Chris Lattner2c7d1772005-04-24 07:28:37 +00001773Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001774 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775}
1776
Chris Lattner2c7d1772005-04-24 07:28:37 +00001777const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1778 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001779}
1780
Dan Gohmana5b96452009-06-04 22:49:04 +00001781Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001782 return cast<BinaryOperator>(BinOp)->getOperand(1);
1783}
1784
1785const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1786 return getFNegArgument(const_cast<Value*>(BinOp));
1787}
1788
Chris Lattner2c7d1772005-04-24 07:28:37 +00001789Value *BinaryOperator::getNotArgument(Value *BinOp) {
1790 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1791 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1792 Value *Op0 = BO->getOperand(0);
1793 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001794 if (isConstantAllOnes(Op0)) return Op1;
1795
1796 assert(isConstantAllOnes(Op1));
1797 return Op0;
1798}
1799
Chris Lattner2c7d1772005-04-24 07:28:37 +00001800const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1801 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001802}
1803
1804
1805// swapOperands - Exchange the two operands to this instruction. This
1806// instruction is safe to use on any binary instruction and does not
1807// modify the semantics of the instruction. If the instruction is
1808// order dependent (SetLT f.e.) the opcode is changed.
1809//
1810bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001811 if (!isCommutative())
1812 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001813 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001814 return false;
1815}
1816
Dan Gohman1b849082009-09-07 23:54:19 +00001817void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1818 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1819}
1820
1821void BinaryOperator::setHasNoSignedWrap(bool b) {
1822 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1823}
1824
1825void BinaryOperator::setIsExact(bool b) {
1826 cast<SDivOperator>(this)->setIsExact(b);
1827}
1828
Nick Lewycky28a5f252009-09-27 21:33:04 +00001829bool BinaryOperator::hasNoUnsignedWrap() const {
1830 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1831}
1832
1833bool BinaryOperator::hasNoSignedWrap() const {
1834 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1835}
1836
1837bool BinaryOperator::isExact() const {
1838 return cast<SDivOperator>(this)->isExact();
1839}
1840
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001841//===----------------------------------------------------------------------===//
1842// CastInst Class
1843//===----------------------------------------------------------------------===//
1844
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845// Just determine if this cast only deals with integral->integral conversion.
1846bool CastInst::isIntegerCast() const {
1847 switch (getOpcode()) {
1848 default: return false;
1849 case Instruction::ZExt:
1850 case Instruction::SExt:
1851 case Instruction::Trunc:
1852 return true;
1853 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001854 return getOperand(0)->getType()->isIntegerTy() &&
1855 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001857}
1858
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001859bool CastInst::isLosslessCast() const {
1860 // Only BitCast can be lossless, exit fast if we're not BitCast
1861 if (getOpcode() != Instruction::BitCast)
1862 return false;
1863
1864 // Identity cast is always lossless
1865 const Type* SrcTy = getOperand(0)->getType();
1866 const Type* DstTy = getType();
1867 if (SrcTy == DstTy)
1868 return true;
1869
Reid Spencer8d9336d2006-12-31 05:26:44 +00001870 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001871 if (SrcTy->isPointerTy())
1872 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001873 return false; // Other types have no identity values
1874}
1875
1876/// This function determines if the CastInst does not require any bits to be
1877/// changed in order to effect the cast. Essentially, it identifies cases where
1878/// no code gen is necessary for the cast, hence the name no-op cast. For
1879/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001880/// # bitcast i32* %x to i8*
1881/// # bitcast <2 x i32> %x to <4 x i16>
1882/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001883/// @brief Determine if the described cast is a no-op.
1884bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1885 const Type *SrcTy,
1886 const Type *DestTy,
1887 const Type *IntPtrTy) {
1888 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 default:
1890 assert(!"Invalid CastOp");
1891 case Instruction::Trunc:
1892 case Instruction::ZExt:
1893 case Instruction::SExt:
1894 case Instruction::FPTrunc:
1895 case Instruction::FPExt:
1896 case Instruction::UIToFP:
1897 case Instruction::SIToFP:
1898 case Instruction::FPToUI:
1899 case Instruction::FPToSI:
1900 return false; // These always modify bits
1901 case Instruction::BitCast:
1902 return true; // BitCast never modifies bits.
1903 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001904 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001905 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001907 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001908 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001909 }
1910}
1911
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001912/// @brief Determine if a cast is a no-op.
1913bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1914 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1915}
1916
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917/// This function determines if a pair of casts can be eliminated and what
1918/// opcode should be used in the elimination. This assumes that there are two
1919/// instructions like this:
1920/// * %F = firstOpcode SrcTy %x to MidTy
1921/// * %S = secondOpcode MidTy %F to DstTy
1922/// The function returns a resultOpcode so these two casts can be replaced with:
1923/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1924/// If no such cast is permited, the function returns 0.
1925unsigned CastInst::isEliminableCastPair(
1926 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1927 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1928{
1929 // Define the 144 possibilities for these two cast instructions. The values
1930 // in this matrix determine what to do in a given situation and select the
1931 // case in the switch below. The rows correspond to firstOp, the columns
1932 // correspond to secondOp. In looking at the table below, keep in mind
1933 // the following cast properties:
1934 //
1935 // Size Compare Source Destination
1936 // Operator Src ? Size Type Sign Type Sign
1937 // -------- ------------ ------------------- ---------------------
1938 // TRUNC > Integer Any Integral Any
1939 // ZEXT < Integral Unsigned Integer Any
1940 // SEXT < Integral Signed Integer Any
1941 // FPTOUI n/a FloatPt n/a Integral Unsigned
1942 // FPTOSI n/a FloatPt n/a Integral Signed
1943 // UITOFP n/a Integral Unsigned FloatPt n/a
1944 // SITOFP n/a Integral Signed FloatPt n/a
1945 // FPTRUNC > FloatPt n/a FloatPt n/a
1946 // FPEXT < FloatPt n/a FloatPt n/a
1947 // PTRTOINT n/a Pointer n/a Integral Unsigned
1948 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001949 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001950 //
1951 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001952 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1953 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001954 // of the produced value (we no longer know the top-part is all zeros).
1955 // Further this conversion is often much more expensive for typical hardware,
1956 // and causes issues when building libgcc. We disallow fptosi+sext for the
1957 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 const unsigned numCastOps =
1959 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1960 static const uint8_t CastResults[numCastOps][numCastOps] = {
1961 // T F F U S F F P I B -+
1962 // R Z S P P I I T P 2 N T |
1963 // U E E 2 2 2 2 R E I T C +- secondOp
1964 // N X X U S F F N X N 2 V |
1965 // C T T I I P P C T T P T -+
1966 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1967 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1968 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001969 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1970 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1972 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1973 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1974 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1975 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1976 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1977 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1978 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001979
1980 // If either of the casts are a bitcast from scalar to vector, disallow the
1981 // merging.
1982 if ((firstOp == Instruction::BitCast &&
1983 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1984 (secondOp == Instruction::BitCast &&
1985 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1986 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987
1988 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1989 [secondOp-Instruction::CastOpsBegin];
1990 switch (ElimCase) {
1991 case 0:
1992 // categorically disallowed
1993 return 0;
1994 case 1:
1995 // allowed, use first cast's opcode
1996 return firstOp;
1997 case 2:
1998 // allowed, use second cast's opcode
1999 return secondOp;
2000 case 3:
2001 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002002 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002003 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002004 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 return firstOp;
2006 return 0;
2007 case 4:
2008 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002009 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 return firstOp;
2012 return 0;
2013 case 5:
2014 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002015 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002016 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017 return secondOp;
2018 return 0;
2019 case 6:
2020 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002021 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002022 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 return secondOp;
2024 return 0;
2025 case 7: {
2026 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002027 if (!IntPtrTy)
2028 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002029 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2030 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031 if (MidSize >= PtrSize)
2032 return Instruction::BitCast;
2033 return 0;
2034 }
2035 case 8: {
2036 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2037 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2038 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002039 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2040 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 if (SrcSize == DstSize)
2042 return Instruction::BitCast;
2043 else if (SrcSize < DstSize)
2044 return firstOp;
2045 return secondOp;
2046 }
2047 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2048 return Instruction::ZExt;
2049 case 10:
2050 // fpext followed by ftrunc is allowed if the bit size returned to is
2051 // the same as the original, in which case its just a bitcast
2052 if (SrcTy == DstTy)
2053 return Instruction::BitCast;
2054 return 0; // If the types are not the same we can't eliminate it.
2055 case 11:
2056 // bitcast followed by ptrtoint is allowed as long as the bitcast
2057 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002058 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 return secondOp;
2060 return 0;
2061 case 12:
2062 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002063 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002064 return firstOp;
2065 return 0;
2066 case 13: {
2067 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002068 if (!IntPtrTy)
2069 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002070 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2071 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2072 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 if (SrcSize <= PtrSize && SrcSize == DstSize)
2074 return Instruction::BitCast;
2075 return 0;
2076 }
2077 case 99:
2078 // cast combination can't happen (error in input). This is for all cases
2079 // where the MidTy is not the same for the two cast instructions.
2080 assert(!"Invalid Cast Combination");
2081 return 0;
2082 default:
2083 assert(!"Error in CastResults table!!!");
2084 return 0;
2085 }
2086 return 0;
2087}
2088
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002089CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002090 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 // Construct and return the appropriate CastInst subclass
2092 switch (op) {
2093 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2094 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2095 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2096 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2097 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2098 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2099 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2100 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2101 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2102 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2103 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2104 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2105 default:
2106 assert(!"Invalid opcode provided");
2107 }
2108 return 0;
2109}
2110
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002111CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002112 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 // Construct and return the appropriate CastInst subclass
2114 switch (op) {
2115 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2116 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2117 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2118 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2119 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2120 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2121 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2122 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2123 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2124 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2125 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2126 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2127 default:
2128 assert(!"Invalid opcode provided");
2129 }
2130 return 0;
2131}
2132
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002133CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002134 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002135 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002136 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002137 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2138 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002139}
2140
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002141CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002142 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002143 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002144 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002145 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2146 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002147}
2148
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002149CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002150 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002151 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002152 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002153 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2154 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002155}
2156
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002157CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002158 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002159 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002160 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002161 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2162 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002163}
2164
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002165CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002166 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002167 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002168 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002169 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2170 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002171}
2172
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002173CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002174 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002175 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002176 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002177 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2178 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002179}
2180
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002181CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002182 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002183 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002184 assert(S->getType()->isPointerTy() && "Invalid cast");
2185 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002186 "Invalid cast");
2187
Duncan Sands9dff9be2010-02-15 16:12:20 +00002188 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002189 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2190 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002191}
2192
2193/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002194CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002195 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002196 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002197 assert(S->getType()->isPointerTy() && "Invalid cast");
2198 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002199 "Invalid cast");
2200
Duncan Sands9dff9be2010-02-15 16:12:20 +00002201 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002202 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2203 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002204}
2205
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002206CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002207 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002208 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002209 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002210 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002211 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2212 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002213 Instruction::CastOps opcode =
2214 (SrcBits == DstBits ? Instruction::BitCast :
2215 (SrcBits > DstBits ? Instruction::Trunc :
2216 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002217 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002218}
2219
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002221 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002222 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002223 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002224 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002225 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2226 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002227 Instruction::CastOps opcode =
2228 (SrcBits == DstBits ? Instruction::BitCast :
2229 (SrcBits > DstBits ? Instruction::Trunc :
2230 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002231 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002232}
2233
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002235 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002236 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002237 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002238 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002239 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2240 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002241 Instruction::CastOps opcode =
2242 (SrcBits == DstBits ? Instruction::BitCast :
2243 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002244 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002245}
2246
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002247CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002248 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002249 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002250 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002251 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002252 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2253 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002254 Instruction::CastOps opcode =
2255 (SrcBits == DstBits ? Instruction::BitCast :
2256 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002257 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002258}
2259
Duncan Sands55e50902008-01-06 10:12:28 +00002260// Check whether it is valid to call getCastOpcode for these types.
2261// This routine must be kept in sync with getCastOpcode.
2262bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2263 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2264 return false;
2265
2266 if (SrcTy == DestTy)
2267 return true;
2268
2269 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002270 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2271 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002272
2273 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002274 if (DestTy->isIntegerTy()) { // Casting to integral
2275 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002276 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002277 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002278 return true;
2279 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002280 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002281 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002282 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002283 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002284 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002285 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2286 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002287 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002288 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002289 return true;
2290 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002291 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002292 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002293 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002294 return false;
2295 }
2296 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002297 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002298 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002299 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002300 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002301 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002302 return DestPTy->getBitWidth() == SrcBits;
2303 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002304 } else if (DestTy->isPointerTy()) { // Casting to pointer
2305 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002306 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002307 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002308 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002309 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002310 return false;
2311 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002312 } else if (DestTy->isX86_MMXTy()) {
2313 return SrcBits == 64;
Gabor Greif697e94c2008-05-15 10:04:30 +00002314 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002315 return false;
2316 }
2317}
2318
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319// Provide a way to get a "cast" where the cast opcode is inferred from the
2320// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002321// logic in the castIsValid function below. This axiom should hold:
2322// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2323// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002325// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002327CastInst::getCastOpcode(
2328 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 // Get the bit sizes, we'll need these
2330 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002331 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2332 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333
Duncan Sands55e50902008-01-06 10:12:28 +00002334 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2335 "Only first class types are castable!");
2336
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002338 if (DestTy->isIntegerTy()) { // Casting to integral
2339 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 if (DestBits < SrcBits)
2341 return Trunc; // int -> smaller int
2342 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002343 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 return SExt; // signed -> SEXT
2345 else
2346 return ZExt; // unsigned -> ZEXT
2347 } else {
2348 return BitCast; // Same size, No-op cast
2349 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002350 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002351 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 return FPToSI; // FP -> sint
2353 else
2354 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002355 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002357 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002358 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 return BitCast; // Same size, no-op cast
2360 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002361 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 "Casting from a value that is not first-class type");
2363 return PtrToInt; // ptr -> int
2364 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002365 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2366 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002367 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 return SIToFP; // sint -> FP
2369 else
2370 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002371 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 if (DestBits < SrcBits) {
2373 return FPTrunc; // FP -> smaller FP
2374 } else if (DestBits > SrcBits) {
2375 return FPExt; // FP -> larger FP
2376 } else {
2377 return BitCast; // same size, no-op cast
2378 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002379 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002381 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002382 PTy = NULL;
2383 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002385 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002387 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2388 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002390 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002391 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002392 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002394 return BitCast; // float/int -> vector
Dale Johannesendd224d22010-09-30 23:57:10 +00002395 } else if (SrcTy->isX86_MMXTy()) {
2396 assert(DestPTy->getBitWidth()==64 &&
2397 "Casting X86_MMX to vector of wrong width");
2398 return BitCast; // MMX to 64-bit vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002400 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002402 } else if (DestTy->isPointerTy()) {
2403 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002405 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 return IntToPtr; // int -> ptr
2407 } else {
2408 assert(!"Casting pointer to other than pointer or int");
2409 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002410 } else if (DestTy->isX86_MMXTy()) {
Bill Wendling3c704172010-10-03 00:46:06 +00002411 if (isa<VectorType>(SrcTy)) {
2412 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
Dale Johannesendd224d22010-09-30 23:57:10 +00002413 "Casting vector of wrong width to X86_MMX");
2414 return BitCast; // 64-bit vector to MMX
2415 } else {
2416 assert(!"Illegal cast to X86_MMX");
2417 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 } else {
2419 assert(!"Casting to type that is not first-class");
2420 }
2421
2422 // If we fall through to here we probably hit an assertion cast above
2423 // and assertions are not turned on. Anything we return is an error, so
2424 // BitCast is as good a choice as any.
2425 return BitCast;
2426}
2427
2428//===----------------------------------------------------------------------===//
2429// CastInst SubClass Constructors
2430//===----------------------------------------------------------------------===//
2431
2432/// Check that the construction parameters for a CastInst are correct. This
2433/// could be broken out into the separate constructors but it is useful to have
2434/// it in one place and to eliminate the redundant code for getting the sizes
2435/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002436bool
2437CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438
2439 // Check for type sanity on the arguments
2440 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002441 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2442 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 return false;
2444
2445 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002446 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2447 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448
2449 // Switch on the opcode provided
2450 switch (op) {
2451 default: return false; // This is an input error
2452 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002453 return SrcTy->isIntOrIntVectorTy() &&
2454 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002456 return SrcTy->isIntOrIntVectorTy() &&
2457 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002459 return SrcTy->isIntOrIntVectorTy() &&
2460 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002462 return SrcTy->isFPOrFPVectorTy() &&
2463 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002464 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002466 return SrcTy->isFPOrFPVectorTy() &&
2467 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002468 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002471 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2472 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002473 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2474 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002475 SVTy->getNumElements() == DVTy->getNumElements();
2476 }
2477 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002478 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002481 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2482 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002483 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2484 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002485 SVTy->getNumElements() == DVTy->getNumElements();
2486 }
2487 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002488 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002490 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002492 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 case Instruction::BitCast:
2494 // BitCast implies a no-op cast of type only. No bits change.
2495 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002496 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 return false;
2498
Duncan Sands55e50902008-01-06 10:12:28 +00002499 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500 // these cases, the cast is okay if the source and destination bit widths
2501 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002502 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503 }
2504}
2505
2506TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002507 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002509 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510}
2511
2512TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002513 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002515 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516}
2517
2518ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002519 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002521 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522}
2523
2524ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002525 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002527 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528}
2529SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002530 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533}
2534
Jeff Cohencc08c832006-12-02 02:22:01 +00002535SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002536 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539}
2540
2541FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002542 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545}
2546
2547FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002548 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002550 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551}
2552
2553FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002554 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557}
2558
2559FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002560 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563}
2564
2565UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002566 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569}
2570
2571UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575}
2576
2577SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002578 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581}
2582
2583SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002584 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
2589FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002590 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593}
2594
2595FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002596 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599}
2600
2601FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002602 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605}
2606
2607FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002608 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
2612
2613PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002614 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617}
2618
2619PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002620 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002622 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623}
2624
2625IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002626 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002628 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629}
2630
2631IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002632 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002634 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635}
2636
2637BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002638 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002640 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641}
2642
2643BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002644 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002646 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002648
2649//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002650// CmpInst Classes
2651//===----------------------------------------------------------------------===//
2652
Chris Lattneraec33da2010-01-22 06:25:37 +00002653void CmpInst::Anchor() const {}
2654
Nate Begemand2195702008-05-12 19:01:56 +00002655CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002657 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002658 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002659 OperandTraits<CmpInst>::op_begin(this),
2660 OperandTraits<CmpInst>::operands(this),
2661 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002662 Op<0>() = LHS;
2663 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002664 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002665 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002666}
Gabor Greiff6caff662008-05-10 08:32:32 +00002667
Nate Begemand2195702008-05-12 19:01:56 +00002668CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002670 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002671 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002672 OperandTraits<CmpInst>::op_begin(this),
2673 OperandTraits<CmpInst>::operands(this),
2674 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002675 Op<0>() = LHS;
2676 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002677 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002678 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002679}
2680
2681CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002682CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002683 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002684 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002685 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002686 if (InsertBefore)
2687 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2688 S1, S2, Name);
2689 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002690 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002691 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002692 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002693
2694 if (InsertBefore)
2695 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2696 S1, S2, Name);
2697 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002698 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002699 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002700}
2701
2702CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002703CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002704 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002705 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002706 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2707 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002708 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002709 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2710 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002711}
2712
2713void CmpInst::swapOperands() {
2714 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2715 IC->swapOperands();
2716 else
2717 cast<FCmpInst>(this)->swapOperands();
2718}
2719
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002720bool CmpInst::isCommutative() const {
2721 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002722 return IC->isCommutative();
2723 return cast<FCmpInst>(this)->isCommutative();
2724}
2725
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002726bool CmpInst::isEquality() const {
2727 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002728 return IC->isEquality();
2729 return cast<FCmpInst>(this)->isEquality();
2730}
2731
2732
Dan Gohman4e724382008-05-31 02:47:54 +00002733CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002734 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002735 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002736 case ICMP_EQ: return ICMP_NE;
2737 case ICMP_NE: return ICMP_EQ;
2738 case ICMP_UGT: return ICMP_ULE;
2739 case ICMP_ULT: return ICMP_UGE;
2740 case ICMP_UGE: return ICMP_ULT;
2741 case ICMP_ULE: return ICMP_UGT;
2742 case ICMP_SGT: return ICMP_SLE;
2743 case ICMP_SLT: return ICMP_SGE;
2744 case ICMP_SGE: return ICMP_SLT;
2745 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002746
Dan Gohman4e724382008-05-31 02:47:54 +00002747 case FCMP_OEQ: return FCMP_UNE;
2748 case FCMP_ONE: return FCMP_UEQ;
2749 case FCMP_OGT: return FCMP_ULE;
2750 case FCMP_OLT: return FCMP_UGE;
2751 case FCMP_OGE: return FCMP_ULT;
2752 case FCMP_OLE: return FCMP_UGT;
2753 case FCMP_UEQ: return FCMP_ONE;
2754 case FCMP_UNE: return FCMP_OEQ;
2755 case FCMP_UGT: return FCMP_OLE;
2756 case FCMP_ULT: return FCMP_OGE;
2757 case FCMP_UGE: return FCMP_OLT;
2758 case FCMP_ULE: return FCMP_OGT;
2759 case FCMP_ORD: return FCMP_UNO;
2760 case FCMP_UNO: return FCMP_ORD;
2761 case FCMP_TRUE: return FCMP_FALSE;
2762 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002763 }
2764}
2765
Reid Spencer266e42b2006-12-23 06:05:41 +00002766ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2767 switch (pred) {
2768 default: assert(! "Unknown icmp predicate!");
2769 case ICMP_EQ: case ICMP_NE:
2770 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2771 return pred;
2772 case ICMP_UGT: return ICMP_SGT;
2773 case ICMP_ULT: return ICMP_SLT;
2774 case ICMP_UGE: return ICMP_SGE;
2775 case ICMP_ULE: return ICMP_SLE;
2776 }
2777}
2778
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002779ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2780 switch (pred) {
2781 default: assert(! "Unknown icmp predicate!");
2782 case ICMP_EQ: case ICMP_NE:
2783 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2784 return pred;
2785 case ICMP_SGT: return ICMP_UGT;
2786 case ICMP_SLT: return ICMP_ULT;
2787 case ICMP_SGE: return ICMP_UGE;
2788 case ICMP_SLE: return ICMP_ULE;
2789 }
2790}
2791
Reid Spencer0286bc12007-02-28 22:00:54 +00002792/// Initialize a set of values that all satisfy the condition with C.
2793///
2794ConstantRange
2795ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2796 APInt Lower(C);
2797 APInt Upper(C);
2798 uint32_t BitWidth = C.getBitWidth();
2799 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002800 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002801 case ICmpInst::ICMP_EQ: Upper++; break;
2802 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002803 case ICmpInst::ICMP_ULT:
2804 Lower = APInt::getMinValue(BitWidth);
2805 // Check for an empty-set condition.
2806 if (Lower == Upper)
2807 return ConstantRange(BitWidth, /*isFullSet=*/false);
2808 break;
2809 case ICmpInst::ICMP_SLT:
2810 Lower = APInt::getSignedMinValue(BitWidth);
2811 // Check for an empty-set condition.
2812 if (Lower == Upper)
2813 return ConstantRange(BitWidth, /*isFullSet=*/false);
2814 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002815 case ICmpInst::ICMP_UGT:
2816 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002817 // Check for an empty-set condition.
2818 if (Lower == Upper)
2819 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002820 break;
2821 case ICmpInst::ICMP_SGT:
2822 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002823 // Check for an empty-set condition.
2824 if (Lower == Upper)
2825 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002826 break;
2827 case ICmpInst::ICMP_ULE:
2828 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002829 // Check for a full-set condition.
2830 if (Lower == Upper)
2831 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002832 break;
2833 case ICmpInst::ICMP_SLE:
2834 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002835 // Check for a full-set condition.
2836 if (Lower == Upper)
2837 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002838 break;
2839 case ICmpInst::ICMP_UGE:
2840 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002841 // Check for a full-set condition.
2842 if (Lower == Upper)
2843 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002844 break;
2845 case ICmpInst::ICMP_SGE:
2846 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002847 // Check for a full-set condition.
2848 if (Lower == Upper)
2849 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002850 break;
2851 }
2852 return ConstantRange(Lower, Upper);
2853}
2854
Dan Gohman4e724382008-05-31 02:47:54 +00002855CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002856 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002857 default: assert(!"Unknown cmp predicate!");
2858 case ICMP_EQ: case ICMP_NE:
2859 return pred;
2860 case ICMP_SGT: return ICMP_SLT;
2861 case ICMP_SLT: return ICMP_SGT;
2862 case ICMP_SGE: return ICMP_SLE;
2863 case ICMP_SLE: return ICMP_SGE;
2864 case ICMP_UGT: return ICMP_ULT;
2865 case ICMP_ULT: return ICMP_UGT;
2866 case ICMP_UGE: return ICMP_ULE;
2867 case ICMP_ULE: return ICMP_UGE;
2868
Reid Spencerd9436b62006-11-20 01:22:35 +00002869 case FCMP_FALSE: case FCMP_TRUE:
2870 case FCMP_OEQ: case FCMP_ONE:
2871 case FCMP_UEQ: case FCMP_UNE:
2872 case FCMP_ORD: case FCMP_UNO:
2873 return pred;
2874 case FCMP_OGT: return FCMP_OLT;
2875 case FCMP_OLT: return FCMP_OGT;
2876 case FCMP_OGE: return FCMP_OLE;
2877 case FCMP_OLE: return FCMP_OGE;
2878 case FCMP_UGT: return FCMP_ULT;
2879 case FCMP_ULT: return FCMP_UGT;
2880 case FCMP_UGE: return FCMP_ULE;
2881 case FCMP_ULE: return FCMP_UGE;
2882 }
2883}
2884
Reid Spencer266e42b2006-12-23 06:05:41 +00002885bool CmpInst::isUnsigned(unsigned short predicate) {
2886 switch (predicate) {
2887 default: return false;
2888 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2889 case ICmpInst::ICMP_UGE: return true;
2890 }
2891}
2892
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002893bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002894 switch (predicate) {
2895 default: return false;
2896 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2897 case ICmpInst::ICMP_SGE: return true;
2898 }
2899}
2900
2901bool CmpInst::isOrdered(unsigned short predicate) {
2902 switch (predicate) {
2903 default: return false;
2904 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2905 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2906 case FCmpInst::FCMP_ORD: return true;
2907 }
2908}
2909
2910bool CmpInst::isUnordered(unsigned short predicate) {
2911 switch (predicate) {
2912 default: return false;
2913 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2914 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2915 case FCmpInst::FCMP_UNO: return true;
2916 }
2917}
2918
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002919bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2920 switch(predicate) {
2921 default: return false;
2922 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2923 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2924 }
2925}
2926
2927bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2928 switch(predicate) {
2929 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2930 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2931 default: return false;
2932 }
2933}
2934
2935
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002936//===----------------------------------------------------------------------===//
2937// SwitchInst Implementation
2938//===----------------------------------------------------------------------===//
2939
Chris Lattnerbaf00152010-11-17 05:41:46 +00002940void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2941 assert(Value && Default && NumReserved);
2942 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002943 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002944 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002945
Gabor Greif2d3024d2008-05-26 21:33:52 +00002946 OperandList[0] = Value;
2947 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002948}
2949
Chris Lattner2195fc42007-02-24 00:55:48 +00002950/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2951/// switch on and a default destination. The number of additional cases can
2952/// be specified here to make memory allocation more efficient. This
2953/// constructor can also autoinsert before another instruction.
2954SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2955 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002956 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2957 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002958 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002959}
2960
2961/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2962/// switch on and a default destination. The number of additional cases can
2963/// be specified here to make memory allocation more efficient. This
2964/// constructor also autoinserts at the end of the specified BasicBlock.
2965SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2966 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002967 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2968 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002969 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002970}
2971
Misha Brukmanb1c93172005-04-21 23:48:37 +00002972SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002973 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2974 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2975 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002977 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002978 OL[i] = InOL[i];
2979 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002980 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002981 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002982}
2983
Gordon Henriksen14a55692007-12-10 02:14:30 +00002984SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002985 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002986}
2987
2988
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002989/// addCase - Add an entry to the switch instruction...
2990///
Chris Lattner47ac1872005-02-24 05:32:09 +00002991void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002992 unsigned OpNo = NumOperands;
2993 if (OpNo+2 > ReservedSpace)
2994 resizeOperands(0); // Get more space!
2995 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002996 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002997 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002998 OperandList[OpNo] = OnVal;
2999 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003000}
3001
3002/// removeCase - This method removes the specified successor from the switch
3003/// instruction. Note that this cannot be used to remove the default
3004/// destination (successor #0).
3005///
3006void SwitchInst::removeCase(unsigned idx) {
3007 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003008 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3009
3010 unsigned NumOps = getNumOperands();
3011 Use *OL = OperandList;
3012
3013 // Move everything after this operand down.
3014 //
3015 // FIXME: we could just swap with the end of the list, then erase. However,
3016 // client might not expect this to happen. The code as it is thrashes the
3017 // use/def lists, which is kinda lame.
3018 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3019 OL[i-2] = OL[i];
3020 OL[i-2+1] = OL[i+1];
3021 }
3022
3023 // Nuke the last value.
3024 OL[NumOps-2].set(0);
3025 OL[NumOps-2+1].set(0);
3026 NumOperands = NumOps-2;
3027}
3028
3029/// resizeOperands - resize operands - This adjusts the length of the operands
3030/// list according to the following behavior:
3031/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003032/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003033/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3034/// 3. If NumOps == NumOperands, trim the reserved space.
3035///
3036void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003037 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003038 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003039 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003040 } else if (NumOps*2 > NumOperands) {
3041 // No resize needed.
3042 if (ReservedSpace >= NumOps) return;
3043 } else if (NumOps == NumOperands) {
3044 if (ReservedSpace == NumOps) return;
3045 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003046 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003047 }
3048
3049 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003050 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003051 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003052 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003053 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003054 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003055 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003056 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003057}
3058
3059
3060BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3061 return getSuccessor(idx);
3062}
3063unsigned SwitchInst::getNumSuccessorsV() const {
3064 return getNumSuccessors();
3065}
3066void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3067 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003068}
Chris Lattnerf22be932004-10-15 23:52:53 +00003069
Chris Lattner3ed871f2009-10-27 19:13:16 +00003070//===----------------------------------------------------------------------===//
3071// SwitchInst Implementation
3072//===----------------------------------------------------------------------===//
3073
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003074void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003075 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003076 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003077 ReservedSpace = 1+NumDests;
3078 NumOperands = 1;
3079 OperandList = allocHungoffUses(ReservedSpace);
3080
3081 OperandList[0] = Address;
3082}
3083
3084
3085/// resizeOperands - resize operands - This adjusts the length of the operands
3086/// list according to the following behavior:
3087/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3088/// of operation. This grows the number of ops by 2 times.
3089/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3090/// 3. If NumOps == NumOperands, trim the reserved space.
3091///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003092void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003093 unsigned e = getNumOperands();
3094 if (NumOps == 0) {
3095 NumOps = e*2;
3096 } else if (NumOps*2 > NumOperands) {
3097 // No resize needed.
3098 if (ReservedSpace >= NumOps) return;
3099 } else if (NumOps == NumOperands) {
3100 if (ReservedSpace == NumOps) return;
3101 } else {
3102 return;
3103 }
3104
3105 ReservedSpace = NumOps;
3106 Use *NewOps = allocHungoffUses(NumOps);
3107 Use *OldOps = OperandList;
3108 for (unsigned i = 0; i != e; ++i)
3109 NewOps[i] = OldOps[i];
3110 OperandList = NewOps;
3111 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3112}
3113
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003114IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3115 Instruction *InsertBefore)
3116: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003117 0, 0, InsertBefore) {
3118 init(Address, NumCases);
3119}
3120
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003121IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3122 BasicBlock *InsertAtEnd)
3123: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003124 0, 0, InsertAtEnd) {
3125 init(Address, NumCases);
3126}
3127
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003128IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3129 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003130 allocHungoffUses(IBI.getNumOperands()),
3131 IBI.getNumOperands()) {
3132 Use *OL = OperandList, *InOL = IBI.OperandList;
3133 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3134 OL[i] = InOL[i];
3135 SubclassOptionalData = IBI.SubclassOptionalData;
3136}
3137
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003138IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003139 dropHungoffUses(OperandList);
3140}
3141
3142/// addDestination - Add a destination.
3143///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003144void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003145 unsigned OpNo = NumOperands;
3146 if (OpNo+1 > ReservedSpace)
3147 resizeOperands(0); // Get more space!
3148 // Initialize some new operands.
3149 assert(OpNo < ReservedSpace && "Growing didn't work!");
3150 NumOperands = OpNo+1;
3151 OperandList[OpNo] = DestBB;
3152}
3153
3154/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003155/// indirectbr instruction.
3156void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003157 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3158
3159 unsigned NumOps = getNumOperands();
3160 Use *OL = OperandList;
3161
3162 // Replace this value with the last one.
3163 OL[idx+1] = OL[NumOps-1];
3164
3165 // Nuke the last value.
3166 OL[NumOps-1].set(0);
3167 NumOperands = NumOps-1;
3168}
3169
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003170BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003171 return getSuccessor(idx);
3172}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003173unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003174 return getNumSuccessors();
3175}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003176void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003177 setSuccessor(idx, B);
3178}
3179
3180//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003181// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003182//===----------------------------------------------------------------------===//
3183
Chris Lattnerf22be932004-10-15 23:52:53 +00003184// Define these methods here so vtables don't get emitted into every translation
3185// unit that uses these classes.
3186
Devang Patel11cf3f42009-10-27 22:16:29 +00003187GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3188 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003189}
3190
Devang Patel11cf3f42009-10-27 22:16:29 +00003191BinaryOperator *BinaryOperator::clone_impl() const {
3192 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003193}
3194
Devang Patel11cf3f42009-10-27 22:16:29 +00003195FCmpInst* FCmpInst::clone_impl() const {
3196 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003197}
3198
Devang Patel11cf3f42009-10-27 22:16:29 +00003199ICmpInst* ICmpInst::clone_impl() const {
3200 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003201}
3202
Devang Patel11cf3f42009-10-27 22:16:29 +00003203ExtractValueInst *ExtractValueInst::clone_impl() const {
3204 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003205}
3206
Devang Patel11cf3f42009-10-27 22:16:29 +00003207InsertValueInst *InsertValueInst::clone_impl() const {
3208 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003209}
3210
Devang Patel11cf3f42009-10-27 22:16:29 +00003211AllocaInst *AllocaInst::clone_impl() const {
3212 return new AllocaInst(getAllocatedType(),
3213 (Value*)getOperand(0),
3214 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217LoadInst *LoadInst::clone_impl() const {
3218 return new LoadInst(getOperand(0),
3219 Twine(), isVolatile(),
3220 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223StoreInst *StoreInst::clone_impl() const {
3224 return new StoreInst(getOperand(0), getOperand(1),
3225 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003226}
3227
Devang Patel11cf3f42009-10-27 22:16:29 +00003228TruncInst *TruncInst::clone_impl() const {
3229 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003230}
3231
Devang Patel11cf3f42009-10-27 22:16:29 +00003232ZExtInst *ZExtInst::clone_impl() const {
3233 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003234}
3235
Devang Patel11cf3f42009-10-27 22:16:29 +00003236SExtInst *SExtInst::clone_impl() const {
3237 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003238}
3239
Devang Patel11cf3f42009-10-27 22:16:29 +00003240FPTruncInst *FPTruncInst::clone_impl() const {
3241 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003242}
3243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244FPExtInst *FPExtInst::clone_impl() const {
3245 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Devang Patel11cf3f42009-10-27 22:16:29 +00003248UIToFPInst *UIToFPInst::clone_impl() const {
3249 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003250}
3251
Devang Patel11cf3f42009-10-27 22:16:29 +00003252SIToFPInst *SIToFPInst::clone_impl() const {
3253 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003254}
3255
Devang Patel11cf3f42009-10-27 22:16:29 +00003256FPToUIInst *FPToUIInst::clone_impl() const {
3257 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258}
3259
Devang Patel11cf3f42009-10-27 22:16:29 +00003260FPToSIInst *FPToSIInst::clone_impl() const {
3261 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264PtrToIntInst *PtrToIntInst::clone_impl() const {
3265 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268IntToPtrInst *IntToPtrInst::clone_impl() const {
3269 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003270}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272BitCastInst *BitCastInst::clone_impl() const {
3273 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003274}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276CallInst *CallInst::clone_impl() const {
3277 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280SelectInst *SelectInst::clone_impl() const {
3281 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003282}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284VAArgInst *VAArgInst::clone_impl() const {
3285 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003286}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288ExtractElementInst *ExtractElementInst::clone_impl() const {
3289 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003290}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292InsertElementInst *InsertElementInst::clone_impl() const {
3293 return InsertElementInst::Create(getOperand(0),
3294 getOperand(1),
3295 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3299 return new ShuffleVectorInst(getOperand(0),
3300 getOperand(1),
3301 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003302}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304PHINode *PHINode::clone_impl() const {
3305 return new PHINode(*this);
3306}
3307
3308ReturnInst *ReturnInst::clone_impl() const {
3309 return new(getNumOperands()) ReturnInst(*this);
3310}
3311
3312BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003313 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003314}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003315
Devang Patel11cf3f42009-10-27 22:16:29 +00003316SwitchInst *SwitchInst::clone_impl() const {
3317 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003318}
3319
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003320IndirectBrInst *IndirectBrInst::clone_impl() const {
3321 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003322}
3323
3324
Devang Patel11cf3f42009-10-27 22:16:29 +00003325InvokeInst *InvokeInst::clone_impl() const {
3326 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003327}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328
Devang Patel11cf3f42009-10-27 22:16:29 +00003329UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003330 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003331 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332}
3333
Devang Patel11cf3f42009-10-27 22:16:29 +00003334UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003335 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003336 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337}