blob: f6abd85202cb4369d224e34fa8f9b3f7069b20c3 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattnerf7b6d312005-05-06 20:26:43 +000025unsigned CallSite::getCallingConv() const {
26 if (CallInst *CI = dyn_cast<CallInst>(I))
27 return CI->getCallingConv();
28 else
29 return cast<InvokeInst>(I)->getCallingConv();
30}
31void CallSite::setCallingConv(unsigned CC) {
32 if (CallInst *CI = dyn_cast<CallInst>(I))
33 CI->setCallingConv(CC);
34 else
35 cast<InvokeInst>(I)->setCallingConv(CC);
36}
37
38
Chris Lattner1c12a882006-06-21 16:53:47 +000039
40
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000041//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000042// TerminatorInst Class
43//===----------------------------------------------------------------------===//
44
Chris Lattner1c12a882006-06-21 16:53:47 +000045// Out of line virtual method, so the vtable, etc has a home.
46TerminatorInst::~TerminatorInst() {
47}
48
49// Out of line virtual method, so the vtable, etc has a home.
50UnaryInstruction::~UnaryInstruction() {
51}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000052
53
54//===----------------------------------------------------------------------===//
55// PHINode Class
56//===----------------------------------------------------------------------===//
57
58PHINode::PHINode(const PHINode &PN)
59 : Instruction(PN.getType(), Instruction::PHI,
60 new Use[PN.getNumOperands()], PN.getNumOperands()),
61 ReservedSpace(PN.getNumOperands()) {
62 Use *OL = OperandList;
63 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
64 OL[i].init(PN.getOperand(i), this);
65 OL[i+1].init(PN.getOperand(i+1), this);
66 }
67}
68
69PHINode::~PHINode() {
70 delete [] OperandList;
71}
72
73// removeIncomingValue - Remove an incoming value. This is useful if a
74// predecessor basic block is deleted.
75Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
76 unsigned NumOps = getNumOperands();
77 Use *OL = OperandList;
78 assert(Idx*2 < NumOps && "BB not in PHI node!");
79 Value *Removed = OL[Idx*2];
80
81 // Move everything after this operand down.
82 //
83 // FIXME: we could just swap with the end of the list, then erase. However,
84 // client might not expect this to happen. The code as it is thrashes the
85 // use/def lists, which is kinda lame.
86 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
87 OL[i-2] = OL[i];
88 OL[i-2+1] = OL[i+1];
89 }
90
91 // Nuke the last value.
92 OL[NumOps-2].set(0);
93 OL[NumOps-2+1].set(0);
94 NumOperands = NumOps-2;
95
96 // If the PHI node is dead, because it has zero entries, nuke it now.
97 if (NumOps == 2 && DeletePHIIfEmpty) {
98 // If anyone is using this PHI, make them use a dummy value instead...
99 replaceAllUsesWith(UndefValue::get(getType()));
100 eraseFromParent();
101 }
102 return Removed;
103}
104
105/// resizeOperands - resize operands - This adjusts the length of the operands
106/// list according to the following behavior:
107/// 1. If NumOps == 0, grow the operand list in response to a push_back style
108/// of operation. This grows the number of ops by 1.5 times.
109/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
110/// 3. If NumOps == NumOperands, trim the reserved space.
111///
112void PHINode::resizeOperands(unsigned NumOps) {
113 if (NumOps == 0) {
114 NumOps = (getNumOperands())*3/2;
115 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
116 } else if (NumOps*2 > NumOperands) {
117 // No resize needed.
118 if (ReservedSpace >= NumOps) return;
119 } else if (NumOps == NumOperands) {
120 if (ReservedSpace == NumOps) return;
121 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123 }
124
125 ReservedSpace = NumOps;
126 Use *NewOps = new Use[NumOps];
127 Use *OldOps = OperandList;
128 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
129 NewOps[i].init(OldOps[i], this);
130 OldOps[i].set(0);
131 }
132 delete [] OldOps;
133 OperandList = NewOps;
134}
135
Nate Begemanb3923212005-08-04 23:24:19 +0000136/// hasConstantValue - If the specified PHI node always merges together the same
137/// value, return the value, otherwise return null.
138///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000139Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000140 // If the PHI node only has one incoming value, eliminate the PHI node...
141 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000142 if (getIncomingValue(0) != this) // not X = phi X
143 return getIncomingValue(0);
144 else
145 return UndefValue::get(getType()); // Self cycle is dead.
146
Nate Begemanb3923212005-08-04 23:24:19 +0000147 // Otherwise if all of the incoming values are the same for the PHI, replace
148 // the PHI node with the incoming value.
149 //
150 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000151 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000152 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000153 if (isa<UndefValue>(getIncomingValue(i)))
154 HasUndefInput = true;
155 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000156 if (InVal && getIncomingValue(i) != InVal)
157 return 0; // Not the same, bail out.
158 else
159 InVal = getIncomingValue(i);
160
161 // The only case that could cause InVal to be null is if we have a PHI node
162 // that only has entries for itself. In this case, there is no entry into the
163 // loop, so kill the PHI.
164 //
165 if (InVal == 0) InVal = UndefValue::get(getType());
166
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000167 // If we have a PHI node like phi(X, undef, X), where X is defined by some
168 // instruction, we cannot always return X as the result of the PHI node. Only
169 // do this if X is not an instruction (thus it must dominate the PHI block),
170 // or if the client is prepared to deal with this possibility.
171 if (HasUndefInput && !AllowNonDominatingInstruction)
172 if (Instruction *IV = dyn_cast<Instruction>(InVal))
173 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000174 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000175 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000176 return 0; // Cannot guarantee that InVal dominates this PHINode.
177
Nate Begemanb3923212005-08-04 23:24:19 +0000178 // All of the incoming values are the same, return the value now.
179 return InVal;
180}
181
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000182
183//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000184// CallInst Implementation
185//===----------------------------------------------------------------------===//
186
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187CallInst::~CallInst() {
188 delete [] OperandList;
Reid Spencerce38beb2007-04-09 18:00:57 +0000189 delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190}
191
Chris Lattner054ba2c2007-02-13 00:58:44 +0000192void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000193 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194 NumOperands = NumParams+1;
195 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000197
Misha Brukmanb1c93172005-04-21 23:48:37 +0000198 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000200 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000201
Chris Lattner054ba2c2007-02-13 00:58:44 +0000202 assert((NumParams == FTy->getNumParams() ||
203 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000204 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000205 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000206 assert((i >= FTy->getNumParams() ||
207 FTy->getParamType(i) == Params[i]->getType()) &&
208 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000209 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000210 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211}
212
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000213void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000214 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 NumOperands = 3;
216 Use *OL = OperandList = new Use[3];
217 OL[0].init(Func, this);
218 OL[1].init(Actual1, this);
219 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000220
221 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000223 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000226 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000228 assert((0 >= FTy->getNumParams() ||
229 FTy->getParamType(0) == Actual1->getType()) &&
230 "Calling a function with a bad signature!");
231 assert((1 >= FTy->getNumParams() ||
232 FTy->getParamType(1) == Actual2->getType()) &&
233 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234}
235
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000236void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000237 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000238 NumOperands = 2;
239 Use *OL = OperandList = new Use[2];
240 OL[0].init(Func, this);
241 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000242
243 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000245 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000247 assert((FTy->getNumParams() == 1 ||
248 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000250 assert((0 == FTy->getNumParams() ||
251 FTy->getParamType(0) == Actual->getType()) &&
252 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253}
254
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000255void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000256 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257 NumOperands = 1;
258 Use *OL = OperandList = new Use[1];
259 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000265 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000268CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000269 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000270 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
271 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000272 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000273 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000274 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000275}
276CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
277 const std::string &Name, Instruction *InsertBefore)
278: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
279 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000280 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000281 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000282 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000283}
284
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
286 const std::string &Name, Instruction *InsertBefore)
287 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
288 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000289 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000291 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
294CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
295 const std::string &Name, BasicBlock *InsertAtEnd)
296 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
297 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000298 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000300 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301}
302
303CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000304 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
306 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000307 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000309 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310}
311
312CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
313 BasicBlock *InsertAtEnd)
314 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
315 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000316 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000318 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
321CallInst::CallInst(Value *Func, const std::string &Name,
322 Instruction *InsertBefore)
323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000327 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328}
329
330CallInst::CallInst(Value *Func, const std::string &Name,
331 BasicBlock *InsertAtEnd)
332 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
333 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000336 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
Misha Brukmanb1c93172005-04-21 23:48:37 +0000339CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000340 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
341 CI.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000342 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000343 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000344 Use *OL = OperandList;
345 Use *InOL = CI.OperandList;
346 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
347 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350
351//===----------------------------------------------------------------------===//
352// InvokeInst Implementation
353//===----------------------------------------------------------------------===//
354
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355InvokeInst::~InvokeInst() {
356 delete [] OperandList;
Reid Spencerce38beb2007-04-09 18:00:57 +0000357 delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000358}
359
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000361 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000362 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000363 NumOperands = 3+NumArgs;
364 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000365 OL[0].init(Fn, this);
366 OL[1].init(IfNormal, this);
367 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000368 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000370 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000371
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000372 assert((NumArgs == FTy->getNumParams()) ||
373 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000375
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000376 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000377 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000378 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000379 "Invoking a function with a bad signature!");
380
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000381 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000382 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383}
384
385InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
386 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000387 Value* const *Args, unsigned NumArgs,
388 const std::string &Name, Instruction *InsertBefore)
389 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
390 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000391 Instruction::Invoke, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000392 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000393 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000394}
395
396InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
397 BasicBlock *IfException,
398 Value* const *Args, unsigned NumArgs,
399 const std::string &Name, BasicBlock *InsertAtEnd)
400 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
401 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000402 Instruction::Invoke, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000403 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000404 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000405}
406
Misha Brukmanb1c93172005-04-21 23:48:37 +0000407InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000408 : TerminatorInst(II.getType(), Instruction::Invoke,
409 new Use[II.getNumOperands()], II.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000410 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000411 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000412 Use *OL = OperandList, *InOL = II.OperandList;
413 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
414 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000415}
416
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
418 return getSuccessor(idx);
419}
420unsigned InvokeInst::getNumSuccessorsV() const {
421 return getNumSuccessors();
422}
423void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
424 return setSuccessor(idx, B);
425}
426
427
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428//===----------------------------------------------------------------------===//
429// ReturnInst Implementation
430//===----------------------------------------------------------------------===//
431
Chris Lattner2195fc42007-02-24 00:55:48 +0000432ReturnInst::ReturnInst(const ReturnInst &RI)
433 : TerminatorInst(Type::VoidTy, Instruction::Ret,
434 &RetVal, RI.getNumOperands()) {
435 if (RI.getNumOperands())
436 RetVal.init(RI.RetVal, this);
437}
438
439ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
440 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
441 init(retVal);
442}
443ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
444 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
445 init(retVal);
446}
447ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
448 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
449}
450
451
452
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000453void ReturnInst::init(Value *retVal) {
454 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000455 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000456 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457 NumOperands = 1;
458 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000459 }
460}
461
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462unsigned ReturnInst::getNumSuccessorsV() const {
463 return getNumSuccessors();
464}
465
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000466// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
467// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000468void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469 assert(0 && "ReturnInst has no successors!");
470}
471
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000472BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
473 assert(0 && "ReturnInst has no successors!");
474 abort();
475 return 0;
476}
477
478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000479//===----------------------------------------------------------------------===//
480// UnwindInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattner2195fc42007-02-24 00:55:48 +0000483UnwindInst::UnwindInst(Instruction *InsertBefore)
484 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
485}
486UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
487 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
488}
489
490
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000491unsigned UnwindInst::getNumSuccessorsV() const {
492 return getNumSuccessors();
493}
494
495void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 assert(0 && "UnwindInst has no successors!");
497}
498
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
500 assert(0 && "UnwindInst has no successors!");
501 abort();
502 return 0;
503}
504
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000505//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000506// UnreachableInst Implementation
507//===----------------------------------------------------------------------===//
508
Chris Lattner2195fc42007-02-24 00:55:48 +0000509UnreachableInst::UnreachableInst(Instruction *InsertBefore)
510 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
511}
512UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
513 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
514}
515
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000516unsigned UnreachableInst::getNumSuccessorsV() const {
517 return getNumSuccessors();
518}
519
520void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
521 assert(0 && "UnwindInst has no successors!");
522}
523
524BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
525 assert(0 && "UnwindInst has no successors!");
526 abort();
527 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000528}
529
530//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000531// BranchInst Implementation
532//===----------------------------------------------------------------------===//
533
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000534void BranchInst::AssertOK() {
535 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000536 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538}
539
Chris Lattner2195fc42007-02-24 00:55:48 +0000540BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
541 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
542 assert(IfTrue != 0 && "Branch destination may not be null!");
543 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
544}
545BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
546 Instruction *InsertBefore)
547: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
548 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
549 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
550 Ops[2].init(Cond, this);
551#ifndef NDEBUG
552 AssertOK();
553#endif
554}
555
556BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
557 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
558 assert(IfTrue != 0 && "Branch destination may not be null!");
559 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
560}
561
562BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
563 BasicBlock *InsertAtEnd)
564 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
565 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
566 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
567 Ops[2].init(Cond, this);
568#ifndef NDEBUG
569 AssertOK();
570#endif
571}
572
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000575 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576 OperandList[0].init(BI.getOperand(0), this);
577 if (BI.getNumOperands() != 1) {
578 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
579 OperandList[1].init(BI.getOperand(1), this);
580 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581 }
582}
583
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
585 return getSuccessor(idx);
586}
587unsigned BranchInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
589}
590void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
591 setSuccessor(idx, B);
592}
593
594
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595//===----------------------------------------------------------------------===//
596// AllocationInst Implementation
597//===----------------------------------------------------------------------===//
598
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000599static Value *getAISize(Value *Amt) {
600 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000601 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000602 else {
603 assert(!isa<BasicBlock>(Amt) &&
604 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000605 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000606 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000607 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000608 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
Misha Brukmanb1c93172005-04-21 23:48:37 +0000611AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000612 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000615 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000616 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000618 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619}
620
Misha Brukmanb1c93172005-04-21 23:48:37 +0000621AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000622 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000625 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000626 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000628 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
Chris Lattner1c12a882006-06-21 16:53:47 +0000631// Out of line virtual method, so the vtable, etc has a home.
632AllocationInst::~AllocationInst() {
633}
634
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000636 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
637 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639}
640
641const Type *AllocationInst::getAllocatedType() const {
642 return getType()->getElementType();
643}
644
645AllocaInst::AllocaInst(const AllocaInst &AI)
646 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000647 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648}
649
650MallocInst::MallocInst(const MallocInst &MI)
651 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000652 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
655//===----------------------------------------------------------------------===//
656// FreeInst Implementation
657//===----------------------------------------------------------------------===//
658
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659void FreeInst::AssertOK() {
660 assert(isa<PointerType>(getOperand(0)->getType()) &&
661 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662}
663
664FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000665 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667}
668
669FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000670 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672}
673
674
675//===----------------------------------------------------------------------===//
676// LoadInst Implementation
677//===----------------------------------------------------------------------===//
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000680 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682}
683
684LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000686 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000687 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000689 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000690}
691
692LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000694 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000695 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000697 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
700LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
701 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000703 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000704 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000706 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
709LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
710 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000712 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000713 setVolatile(isVolatile);
714 AssertOK();
715 setName(Name);
716}
717
718
719
720LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000721 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
722 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000723 setVolatile(false);
724 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000725 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000726}
727
728LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000729 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
730 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000731 setVolatile(false);
732 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000734}
735
736LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
737 Instruction *InsertBef)
738: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000739 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000740 setVolatile(isVolatile);
741 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000742 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000743}
744
745LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
746 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000747 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
748 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000749 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000751 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
754
755//===----------------------------------------------------------------------===//
756// StoreInst Implementation
757//===----------------------------------------------------------------------===//
758
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000759void StoreInst::AssertOK() {
760 assert(isa<PointerType>(getOperand(1)->getType()) &&
761 "Ptr must have pointer type!");
762 assert(getOperand(0)->getType() ==
763 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000764 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767
768StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000769 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770 Ops[0].init(val, this);
771 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000772 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773 AssertOK();
774}
775
776StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000777 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 Ops[0].init(val, this);
779 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000780 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781 AssertOK();
782}
783
Misha Brukmanb1c93172005-04-21 23:48:37 +0000784StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000786 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787 Ops[0].init(val, this);
788 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000789 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790 AssertOK();
791}
792
Misha Brukmanb1c93172005-04-21 23:48:37 +0000793StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000795 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 Ops[0].init(val, this);
797 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000798 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800}
801
802//===----------------------------------------------------------------------===//
803// GetElementPtrInst Implementation
804//===----------------------------------------------------------------------===//
805
806// checkType - Simple wrapper function to give a better assertion failure
807// message on bad indexes for a gep instruction.
808//
809static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000810 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811 return Ty;
812}
813
Chris Lattner79807c3d2007-01-31 19:47:18 +0000814void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
815 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 Use *OL = OperandList = new Use[NumOperands];
817 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818
Chris Lattner79807c3d2007-01-31 19:47:18 +0000819 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821}
822
823void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 NumOperands = 3;
825 Use *OL = OperandList = new Use[3];
826 OL[0].init(Ptr, this);
827 OL[1].init(Idx0, this);
828 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000829}
830
Chris Lattner82981202005-05-03 05:43:30 +0000831void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
832 NumOperands = 2;
833 Use *OL = OperandList = new Use[2];
834 OL[0].init(Ptr, this);
835 OL[1].init(Idx, this);
836}
837
Chris Lattner79807c3d2007-01-31 19:47:18 +0000838
839GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
840 unsigned NumIdx,
841 const std::string &Name, Instruction *InBe)
842: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000843 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 GetElementPtr, 0, 0, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000845 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000846 setName(Name);
Chris Lattner79807c3d2007-01-31 19:47:18 +0000847}
848
849GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
850 unsigned NumIdx,
851 const std::string &Name, BasicBlock *IAE)
852: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000853 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 GetElementPtr, 0, 0, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000855 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857}
858
Chris Lattner82981202005-05-03 05:43:30 +0000859GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
860 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000861 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
862 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000863 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000865}
866
867GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
868 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000869 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
870 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000871 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000873}
874
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
876 const std::string &Name, Instruction *InBe)
877 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
878 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 GetElementPtr, 0, 0, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000881 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882}
883
884GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000885 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
887 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 GetElementPtr, 0, 0, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000890 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000891}
892
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000893GetElementPtrInst::~GetElementPtrInst() {
894 delete[] OperandList;
895}
896
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000897// getIndexedType - Returns the type of the element that would be loaded with
898// a load instruction with the specified parameters.
899//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000900// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901// pointer type.
902//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000903const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000904 Value* const *Idxs,
905 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906 bool AllowCompositeLeaf) {
907 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
908
909 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000910 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911 if (AllowCompositeLeaf ||
912 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
913 return cast<PointerType>(Ptr)->getElementType();
914 else
915 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000916
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917 unsigned CurIdx = 0;
918 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000919 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
921 return 0; // Can't load a whole structure or array!?!?
922 }
923
Chris Lattner302116a2007-01-31 04:40:28 +0000924 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925 if (isa<PointerType>(CT) && CurIdx != 1)
926 return 0; // Can only index into pointer types at the first index!
927 if (!CT->indexValid(Index)) return 0;
928 Ptr = CT->getTypeAtIndex(Index);
929
930 // If the new type forwards to another type, then it is in the middle
931 // of being refined to another type (and hence, may have dropped all
932 // references to what it was using before). So, use the new forwarded
933 // type.
934 if (const Type * Ty = Ptr->getForwardedType()) {
935 Ptr = Ty;
936 }
937 }
Chris Lattner302116a2007-01-31 04:40:28 +0000938 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000939}
940
Misha Brukmanb1c93172005-04-21 23:48:37 +0000941const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000942 Value *Idx0, Value *Idx1,
943 bool AllowCompositeLeaf) {
944 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
945 if (!PTy) return 0; // Type isn't a pointer type!
946
947 // Check the pointer index.
948 if (!PTy->indexValid(Idx0)) return 0;
949
950 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
951 if (!CT || !CT->indexValid(Idx1)) return 0;
952
953 const Type *ElTy = CT->getTypeAtIndex(Idx1);
954 if (AllowCompositeLeaf || ElTy->isFirstClassType())
955 return ElTy;
956 return 0;
957}
958
Chris Lattner82981202005-05-03 05:43:30 +0000959const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
960 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
961 if (!PTy) return 0; // Type isn't a pointer type!
962
963 // Check the pointer index.
964 if (!PTy->indexValid(Idx)) return 0;
965
Chris Lattnerc2233332005-05-03 16:44:45 +0000966 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000967}
968
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000970// ExtractElementInst Implementation
971//===----------------------------------------------------------------------===//
972
973ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000974 const std::string &Name,
975 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000976 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000977 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000978 assert(isValidOperands(Val, Index) &&
979 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000980 Ops[0].init(Val, this);
981 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000982 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +0000983}
984
Chris Lattner65511ff2006-10-05 06:24:58 +0000985ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
986 const std::string &Name,
987 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000988 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000989 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000990 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000991 assert(isValidOperands(Val, Index) &&
992 "Invalid extractelement instruction operands!");
993 Ops[0].init(Val, this);
994 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000995 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +0000996}
997
998
Robert Bocchino23004482006-01-10 19:05:34 +0000999ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001000 const std::string &Name,
1001 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001002 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001003 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001004 assert(isValidOperands(Val, Index) &&
1005 "Invalid extractelement instruction operands!");
1006
Robert Bocchino23004482006-01-10 19:05:34 +00001007 Ops[0].init(Val, this);
1008 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001009 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001010}
1011
Chris Lattner65511ff2006-10-05 06:24:58 +00001012ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1013 const std::string &Name,
1014 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001015 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001016 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001017 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001018 assert(isValidOperands(Val, Index) &&
1019 "Invalid extractelement instruction operands!");
1020
1021 Ops[0].init(Val, this);
1022 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001023 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001024}
1025
1026
Chris Lattner54865b32006-04-08 04:05:48 +00001027bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001028 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001029 return false;
1030 return true;
1031}
1032
1033
Robert Bocchino23004482006-01-10 19:05:34 +00001034//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001035// InsertElementInst Implementation
1036//===----------------------------------------------------------------------===//
1037
Chris Lattner0875d942006-04-14 22:20:32 +00001038InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1039 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1040 Ops[0].init(IE.Ops[0], this);
1041 Ops[1].init(IE.Ops[1], this);
1042 Ops[2].init(IE.Ops[2], this);
1043}
Chris Lattner54865b32006-04-08 04:05:48 +00001044InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001045 const std::string &Name,
1046 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001047 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001048 assert(isValidOperands(Vec, Elt, Index) &&
1049 "Invalid insertelement instruction operands!");
1050 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001051 Ops[1].init(Elt, this);
1052 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001053 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001054}
1055
Chris Lattner65511ff2006-10-05 06:24:58 +00001056InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1057 const std::string &Name,
1058 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001059 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001060 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001061 assert(isValidOperands(Vec, Elt, Index) &&
1062 "Invalid insertelement instruction operands!");
1063 Ops[0].init(Vec, this);
1064 Ops[1].init(Elt, this);
1065 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001066 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001067}
1068
1069
Chris Lattner54865b32006-04-08 04:05:48 +00001070InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001071 const std::string &Name,
1072 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001073 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001074 assert(isValidOperands(Vec, Elt, Index) &&
1075 "Invalid insertelement instruction operands!");
1076
1077 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001078 Ops[1].init(Elt, this);
1079 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001080 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001081}
1082
Chris Lattner65511ff2006-10-05 06:24:58 +00001083InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1084 const std::string &Name,
1085 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001086: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001087 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001088 assert(isValidOperands(Vec, Elt, Index) &&
1089 "Invalid insertelement instruction operands!");
1090
1091 Ops[0].init(Vec, this);
1092 Ops[1].init(Elt, this);
1093 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001094 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001095}
1096
Chris Lattner54865b32006-04-08 04:05:48 +00001097bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1098 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001099 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001100 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001101
Reid Spencerd84d35b2007-02-15 02:26:10 +00001102 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Chris Lattner54865b32006-04-08 04:05:48 +00001103 return false;// Second operand of insertelement must be packed element type.
1104
Reid Spencer8d9336d2006-12-31 05:26:44 +00001105 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001106 return false; // Third operand of insertelement must be uint.
1107 return true;
1108}
1109
1110
Robert Bocchinoca27f032006-01-17 20:07:22 +00001111//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001112// ShuffleVectorInst Implementation
1113//===----------------------------------------------------------------------===//
1114
Chris Lattner0875d942006-04-14 22:20:32 +00001115ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1116 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1117 Ops[0].init(SV.Ops[0], this);
1118 Ops[1].init(SV.Ops[1], this);
1119 Ops[2].init(SV.Ops[2], this);
1120}
1121
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001122ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1123 const std::string &Name,
1124 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001125 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001126 assert(isValidOperands(V1, V2, Mask) &&
1127 "Invalid shuffle vector instruction operands!");
1128 Ops[0].init(V1, this);
1129 Ops[1].init(V2, this);
1130 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001132}
1133
1134ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1135 const std::string &Name,
1136 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001137 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001138 assert(isValidOperands(V1, V2, Mask) &&
1139 "Invalid shuffle vector instruction operands!");
1140
1141 Ops[0].init(V1, this);
1142 Ops[1].init(V2, this);
1143 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001144 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001145}
1146
1147bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1148 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001149 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001150 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001151 if (!isa<VectorType>(Mask->getType()) ||
1152 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1153 cast<VectorType>(Mask->getType())->getNumElements() !=
1154 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001155 return false;
1156 return true;
1157}
1158
1159
1160//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001161// BinaryOperator Class
1162//===----------------------------------------------------------------------===//
1163
Chris Lattner2195fc42007-02-24 00:55:48 +00001164BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1165 const Type *Ty, const std::string &Name,
1166 Instruction *InsertBefore)
1167 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1168 Ops[0].init(S1, this);
1169 Ops[1].init(S2, this);
1170 init(iType);
1171 setName(Name);
1172}
1173
1174BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1175 const Type *Ty, const std::string &Name,
1176 BasicBlock *InsertAtEnd)
1177 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1178 Ops[0].init(S1, this);
1179 Ops[1].init(S2, this);
1180 init(iType);
1181 setName(Name);
1182}
1183
1184
1185void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001186 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001187 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001188 assert(LHS->getType() == RHS->getType() &&
1189 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190#ifndef NDEBUG
1191 switch (iType) {
1192 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001193 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001194 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001195 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001196 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001197 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001198 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001199 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001200 case UDiv:
1201 case SDiv:
1202 assert(getType() == LHS->getType() &&
1203 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001204 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1205 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001206 "Incorrect operand type (not integer) for S/UDIV");
1207 break;
1208 case FDiv:
1209 assert(getType() == LHS->getType() &&
1210 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001211 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1212 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001213 && "Incorrect operand type (not floating point) for FDIV");
1214 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001215 case URem:
1216 case SRem:
1217 assert(getType() == LHS->getType() &&
1218 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001219 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1220 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001221 "Incorrect operand type (not integer) for S/UREM");
1222 break;
1223 case FRem:
1224 assert(getType() == LHS->getType() &&
1225 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001226 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1227 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001228 && "Incorrect operand type (not floating point) for FREM");
1229 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001230 case Shl:
1231 case LShr:
1232 case AShr:
1233 assert(getType() == LHS->getType() &&
1234 "Shift operation should return same type as operands!");
1235 assert(getType()->isInteger() &&
1236 "Shift operation requires integer operands");
1237 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001238 case And: case Or:
1239 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001240 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001242 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001243 (isa<VectorType>(getType()) &&
1244 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001245 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001246 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001247 default:
1248 break;
1249 }
1250#endif
1251}
1252
1253BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001254 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001255 Instruction *InsertBefore) {
1256 assert(S1->getType() == S2->getType() &&
1257 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001258 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001259}
1260
1261BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001262 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001263 BasicBlock *InsertAtEnd) {
1264 BinaryOperator *Res = create(Op, S1, S2, Name);
1265 InsertAtEnd->getInstList().push_back(Res);
1266 return Res;
1267}
1268
1269BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1270 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001271 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1272 return new BinaryOperator(Instruction::Sub,
1273 zero, Op,
1274 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001275}
1276
1277BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1278 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001279 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1280 return new BinaryOperator(Instruction::Sub,
1281 zero, Op,
1282 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001283}
1284
1285BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1286 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001287 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001288 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001289 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001290 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001291 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001292 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001293 }
1294
1295 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296 Op->getType(), Name, InsertBefore);
1297}
1298
1299BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1300 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001301 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001302 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001303 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001304 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001305 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001307 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001308 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001309 }
1310
1311 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001312 Op->getType(), Name, InsertAtEnd);
1313}
1314
1315
1316// isConstantAllOnes - Helper function for several functions below
1317static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001318 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319}
1320
1321bool BinaryOperator::isNeg(const Value *V) {
1322 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1323 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001324 return Bop->getOperand(0) ==
1325 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001326 return false;
1327}
1328
1329bool BinaryOperator::isNot(const Value *V) {
1330 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1331 return (Bop->getOpcode() == Instruction::Xor &&
1332 (isConstantAllOnes(Bop->getOperand(1)) ||
1333 isConstantAllOnes(Bop->getOperand(0))));
1334 return false;
1335}
1336
Chris Lattner2c7d1772005-04-24 07:28:37 +00001337Value *BinaryOperator::getNegArgument(Value *BinOp) {
1338 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1339 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001340}
1341
Chris Lattner2c7d1772005-04-24 07:28:37 +00001342const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1343 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001344}
1345
Chris Lattner2c7d1772005-04-24 07:28:37 +00001346Value *BinaryOperator::getNotArgument(Value *BinOp) {
1347 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1348 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1349 Value *Op0 = BO->getOperand(0);
1350 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001351 if (isConstantAllOnes(Op0)) return Op1;
1352
1353 assert(isConstantAllOnes(Op1));
1354 return Op0;
1355}
1356
Chris Lattner2c7d1772005-04-24 07:28:37 +00001357const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1358 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001359}
1360
1361
1362// swapOperands - Exchange the two operands to this instruction. This
1363// instruction is safe to use on any binary instruction and does not
1364// modify the semantics of the instruction. If the instruction is
1365// order dependent (SetLT f.e.) the opcode is changed.
1366//
1367bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001368 if (!isCommutative())
1369 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001370 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001371 return false;
1372}
1373
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001374//===----------------------------------------------------------------------===//
1375// CastInst Class
1376//===----------------------------------------------------------------------===//
1377
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001378// Just determine if this cast only deals with integral->integral conversion.
1379bool CastInst::isIntegerCast() const {
1380 switch (getOpcode()) {
1381 default: return false;
1382 case Instruction::ZExt:
1383 case Instruction::SExt:
1384 case Instruction::Trunc:
1385 return true;
1386 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001387 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001389}
1390
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001391bool CastInst::isLosslessCast() const {
1392 // Only BitCast can be lossless, exit fast if we're not BitCast
1393 if (getOpcode() != Instruction::BitCast)
1394 return false;
1395
1396 // Identity cast is always lossless
1397 const Type* SrcTy = getOperand(0)->getType();
1398 const Type* DstTy = getType();
1399 if (SrcTy == DstTy)
1400 return true;
1401
Reid Spencer8d9336d2006-12-31 05:26:44 +00001402 // Pointer to pointer is always lossless.
1403 if (isa<PointerType>(SrcTy))
1404 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001405 return false; // Other types have no identity values
1406}
1407
1408/// This function determines if the CastInst does not require any bits to be
1409/// changed in order to effect the cast. Essentially, it identifies cases where
1410/// no code gen is necessary for the cast, hence the name no-op cast. For
1411/// example, the following are all no-op casts:
1412/// # bitcast uint %X, int
1413/// # bitcast uint* %x, sbyte*
1414/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1415/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1416/// @brief Determine if a cast is a no-op.
1417bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1418 switch (getOpcode()) {
1419 default:
1420 assert(!"Invalid CastOp");
1421 case Instruction::Trunc:
1422 case Instruction::ZExt:
1423 case Instruction::SExt:
1424 case Instruction::FPTrunc:
1425 case Instruction::FPExt:
1426 case Instruction::UIToFP:
1427 case Instruction::SIToFP:
1428 case Instruction::FPToUI:
1429 case Instruction::FPToSI:
1430 return false; // These always modify bits
1431 case Instruction::BitCast:
1432 return true; // BitCast never modifies bits.
1433 case Instruction::PtrToInt:
1434 return IntPtrTy->getPrimitiveSizeInBits() ==
1435 getType()->getPrimitiveSizeInBits();
1436 case Instruction::IntToPtr:
1437 return IntPtrTy->getPrimitiveSizeInBits() ==
1438 getOperand(0)->getType()->getPrimitiveSizeInBits();
1439 }
1440}
1441
1442/// This function determines if a pair of casts can be eliminated and what
1443/// opcode should be used in the elimination. This assumes that there are two
1444/// instructions like this:
1445/// * %F = firstOpcode SrcTy %x to MidTy
1446/// * %S = secondOpcode MidTy %F to DstTy
1447/// The function returns a resultOpcode so these two casts can be replaced with:
1448/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1449/// If no such cast is permited, the function returns 0.
1450unsigned CastInst::isEliminableCastPair(
1451 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1452 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1453{
1454 // Define the 144 possibilities for these two cast instructions. The values
1455 // in this matrix determine what to do in a given situation and select the
1456 // case in the switch below. The rows correspond to firstOp, the columns
1457 // correspond to secondOp. In looking at the table below, keep in mind
1458 // the following cast properties:
1459 //
1460 // Size Compare Source Destination
1461 // Operator Src ? Size Type Sign Type Sign
1462 // -------- ------------ ------------------- ---------------------
1463 // TRUNC > Integer Any Integral Any
1464 // ZEXT < Integral Unsigned Integer Any
1465 // SEXT < Integral Signed Integer Any
1466 // FPTOUI n/a FloatPt n/a Integral Unsigned
1467 // FPTOSI n/a FloatPt n/a Integral Signed
1468 // UITOFP n/a Integral Unsigned FloatPt n/a
1469 // SITOFP n/a Integral Signed FloatPt n/a
1470 // FPTRUNC > FloatPt n/a FloatPt n/a
1471 // FPEXT < FloatPt n/a FloatPt n/a
1472 // PTRTOINT n/a Pointer n/a Integral Unsigned
1473 // INTTOPTR n/a Integral Unsigned Pointer n/a
1474 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001475 //
1476 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1477 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1478 // into "fptoui double to ulong", but this loses information about the range
1479 // of the produced value (we no longer know the top-part is all zeros).
1480 // Further this conversion is often much more expensive for typical hardware,
1481 // and causes issues when building libgcc. We disallow fptosi+sext for the
1482 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001483 const unsigned numCastOps =
1484 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1485 static const uint8_t CastResults[numCastOps][numCastOps] = {
1486 // T F F U S F F P I B -+
1487 // R Z S P P I I T P 2 N T |
1488 // U E E 2 2 2 2 R E I T C +- secondOp
1489 // N X X U S F F N X N 2 V |
1490 // C T T I I P P C T T P T -+
1491 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1492 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1493 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001494 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1495 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001496 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1497 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1498 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1499 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1500 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1501 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1502 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1503 };
1504
1505 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1506 [secondOp-Instruction::CastOpsBegin];
1507 switch (ElimCase) {
1508 case 0:
1509 // categorically disallowed
1510 return 0;
1511 case 1:
1512 // allowed, use first cast's opcode
1513 return firstOp;
1514 case 2:
1515 // allowed, use second cast's opcode
1516 return secondOp;
1517 case 3:
1518 // no-op cast in second op implies firstOp as long as the DestTy
1519 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001520 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001521 return firstOp;
1522 return 0;
1523 case 4:
1524 // no-op cast in second op implies firstOp as long as the DestTy
1525 // is floating point
1526 if (DstTy->isFloatingPoint())
1527 return firstOp;
1528 return 0;
1529 case 5:
1530 // no-op cast in first op implies secondOp as long as the SrcTy
1531 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001532 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001533 return secondOp;
1534 return 0;
1535 case 6:
1536 // no-op cast in first op implies secondOp as long as the SrcTy
1537 // is a floating point
1538 if (SrcTy->isFloatingPoint())
1539 return secondOp;
1540 return 0;
1541 case 7: {
1542 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1543 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1544 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1545 if (MidSize >= PtrSize)
1546 return Instruction::BitCast;
1547 return 0;
1548 }
1549 case 8: {
1550 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1551 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1552 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1553 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1554 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1555 if (SrcSize == DstSize)
1556 return Instruction::BitCast;
1557 else if (SrcSize < DstSize)
1558 return firstOp;
1559 return secondOp;
1560 }
1561 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1562 return Instruction::ZExt;
1563 case 10:
1564 // fpext followed by ftrunc is allowed if the bit size returned to is
1565 // the same as the original, in which case its just a bitcast
1566 if (SrcTy == DstTy)
1567 return Instruction::BitCast;
1568 return 0; // If the types are not the same we can't eliminate it.
1569 case 11:
1570 // bitcast followed by ptrtoint is allowed as long as the bitcast
1571 // is a pointer to pointer cast.
1572 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1573 return secondOp;
1574 return 0;
1575 case 12:
1576 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1577 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1578 return firstOp;
1579 return 0;
1580 case 13: {
1581 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1582 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1583 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1584 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1585 if (SrcSize <= PtrSize && SrcSize == DstSize)
1586 return Instruction::BitCast;
1587 return 0;
1588 }
1589 case 99:
1590 // cast combination can't happen (error in input). This is for all cases
1591 // where the MidTy is not the same for the two cast instructions.
1592 assert(!"Invalid Cast Combination");
1593 return 0;
1594 default:
1595 assert(!"Error in CastResults table!!!");
1596 return 0;
1597 }
1598 return 0;
1599}
1600
1601CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1602 const std::string &Name, Instruction *InsertBefore) {
1603 // Construct and return the appropriate CastInst subclass
1604 switch (op) {
1605 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1606 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1607 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1608 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1609 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1610 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1611 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1612 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1613 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1614 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1615 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1616 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1617 default:
1618 assert(!"Invalid opcode provided");
1619 }
1620 return 0;
1621}
1622
1623CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1624 const std::string &Name, BasicBlock *InsertAtEnd) {
1625 // Construct and return the appropriate CastInst subclass
1626 switch (op) {
1627 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1628 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1629 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1630 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1631 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1632 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1633 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1634 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1635 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1636 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1637 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1638 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1639 default:
1640 assert(!"Invalid opcode provided");
1641 }
1642 return 0;
1643}
1644
Reid Spencer5c140882006-12-04 20:17:56 +00001645CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1646 const std::string &Name,
1647 Instruction *InsertBefore) {
1648 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1649 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1650 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1651}
1652
1653CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1654 const std::string &Name,
1655 BasicBlock *InsertAtEnd) {
1656 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1657 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1658 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1659}
1660
1661CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1662 const std::string &Name,
1663 Instruction *InsertBefore) {
1664 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1665 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1666 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1667}
1668
1669CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1670 const std::string &Name,
1671 BasicBlock *InsertAtEnd) {
1672 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1673 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1674 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1675}
1676
1677CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1678 const std::string &Name,
1679 Instruction *InsertBefore) {
1680 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1681 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1682 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1683}
1684
1685CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1686 const std::string &Name,
1687 BasicBlock *InsertAtEnd) {
1688 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1689 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1690 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1691}
1692
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001693CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1694 const std::string &Name,
1695 BasicBlock *InsertAtEnd) {
1696 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001697 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001698 "Invalid cast");
1699
Chris Lattner03c49532007-01-15 02:27:26 +00001700 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001701 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1702 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1703}
1704
1705/// @brief Create a BitCast or a PtrToInt cast instruction
1706CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1707 const std::string &Name,
1708 Instruction *InsertBefore) {
1709 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001710 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001711 "Invalid cast");
1712
Chris Lattner03c49532007-01-15 02:27:26 +00001713 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001714 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1715 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1716}
1717
Reid Spencer7e933472006-12-12 00:49:44 +00001718CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1719 bool isSigned, const std::string &Name,
1720 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001721 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001722 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1723 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1724 Instruction::CastOps opcode =
1725 (SrcBits == DstBits ? Instruction::BitCast :
1726 (SrcBits > DstBits ? Instruction::Trunc :
1727 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1728 return create(opcode, C, Ty, Name, InsertBefore);
1729}
1730
1731CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1732 bool isSigned, const std::string &Name,
1733 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001734 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001735 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1736 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1737 Instruction::CastOps opcode =
1738 (SrcBits == DstBits ? Instruction::BitCast :
1739 (SrcBits > DstBits ? Instruction::Trunc :
1740 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1741 return create(opcode, C, Ty, Name, InsertAtEnd);
1742}
1743
1744CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1745 const std::string &Name,
1746 Instruction *InsertBefore) {
1747 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1748 "Invalid cast");
1749 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1750 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1751 Instruction::CastOps opcode =
1752 (SrcBits == DstBits ? Instruction::BitCast :
1753 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1754 return create(opcode, C, Ty, Name, InsertBefore);
1755}
1756
1757CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1758 const std::string &Name,
1759 BasicBlock *InsertAtEnd) {
1760 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1761 "Invalid cast");
1762 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1763 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1764 Instruction::CastOps opcode =
1765 (SrcBits == DstBits ? Instruction::BitCast :
1766 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1767 return create(opcode, C, Ty, Name, InsertAtEnd);
1768}
1769
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001770// Provide a way to get a "cast" where the cast opcode is inferred from the
1771// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001772// logic in the castIsValid function below. This axiom should hold:
1773// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1774// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001775// casting opcode for the arguments passed to it.
1776Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001777CastInst::getCastOpcode(
1778 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001779 // Get the bit sizes, we'll need these
1780 const Type *SrcTy = Src->getType();
1781 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1782 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1783
1784 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001785 if (DestTy->isInteger()) { // Casting to integral
1786 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001787 if (DestBits < SrcBits)
1788 return Trunc; // int -> smaller int
1789 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001790 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001791 return SExt; // signed -> SEXT
1792 else
1793 return ZExt; // unsigned -> ZEXT
1794 } else {
1795 return BitCast; // Same size, No-op cast
1796 }
1797 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001798 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001799 return FPToSI; // FP -> sint
1800 else
1801 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001802 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001803 assert(DestBits == PTy->getBitWidth() &&
1804 "Casting packed to integer of different width");
1805 return BitCast; // Same size, no-op cast
1806 } else {
1807 assert(isa<PointerType>(SrcTy) &&
1808 "Casting from a value that is not first-class type");
1809 return PtrToInt; // ptr -> int
1810 }
1811 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001812 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001813 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814 return SIToFP; // sint -> FP
1815 else
1816 return UIToFP; // uint -> FP
1817 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1818 if (DestBits < SrcBits) {
1819 return FPTrunc; // FP -> smaller FP
1820 } else if (DestBits > SrcBits) {
1821 return FPExt; // FP -> larger FP
1822 } else {
1823 return BitCast; // same size, no-op cast
1824 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001825 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001826 assert(DestBits == PTy->getBitWidth() &&
1827 "Casting packed to floating point of different width");
1828 return BitCast; // same size, no-op cast
1829 } else {
1830 assert(0 && "Casting pointer or non-first class to float");
1831 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001832 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1833 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001834 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1835 "Casting packed to packed of different widths");
1836 return BitCast; // packed -> packed
1837 } else if (DestPTy->getBitWidth() == SrcBits) {
1838 return BitCast; // float/int -> packed
1839 } else {
1840 assert(!"Illegal cast to packed (wrong type or size)");
1841 }
1842 } else if (isa<PointerType>(DestTy)) {
1843 if (isa<PointerType>(SrcTy)) {
1844 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001845 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 return IntToPtr; // int -> ptr
1847 } else {
1848 assert(!"Casting pointer to other than pointer or int");
1849 }
1850 } else {
1851 assert(!"Casting to type that is not first-class");
1852 }
1853
1854 // If we fall through to here we probably hit an assertion cast above
1855 // and assertions are not turned on. Anything we return is an error, so
1856 // BitCast is as good a choice as any.
1857 return BitCast;
1858}
1859
1860//===----------------------------------------------------------------------===//
1861// CastInst SubClass Constructors
1862//===----------------------------------------------------------------------===//
1863
1864/// Check that the construction parameters for a CastInst are correct. This
1865/// could be broken out into the separate constructors but it is useful to have
1866/// it in one place and to eliminate the redundant code for getting the sizes
1867/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001868bool
1869CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870
1871 // Check for type sanity on the arguments
1872 const Type *SrcTy = S->getType();
1873 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1874 return false;
1875
1876 // Get the size of the types in bits, we'll need this later
1877 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1878 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1879
1880 // Switch on the opcode provided
1881 switch (op) {
1882 default: return false; // This is an input error
1883 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001884 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001886 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001888 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 case Instruction::FPTrunc:
1890 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1891 SrcBitSize > DstBitSize;
1892 case Instruction::FPExt:
1893 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1894 SrcBitSize < DstBitSize;
1895 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001896 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001898 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001900 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001902 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001904 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001906 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001907 case Instruction::BitCast:
1908 // BitCast implies a no-op cast of type only. No bits change.
1909 // However, you can't cast pointers to anything but pointers.
1910 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1911 return false;
1912
1913 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1914 // these cases, the cast is okay if the source and destination bit widths
1915 // are identical.
1916 return SrcBitSize == DstBitSize;
1917 }
1918}
1919
1920TruncInst::TruncInst(
1921 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1922) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001923 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924}
1925
1926TruncInst::TruncInst(
1927 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1928) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001929 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001930}
1931
1932ZExtInst::ZExtInst(
1933 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1934) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001935 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936}
1937
1938ZExtInst::ZExtInst(
1939 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1940) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001941 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942}
1943SExtInst::SExtInst(
1944 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1945) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001946 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947}
1948
Jeff Cohencc08c832006-12-02 02:22:01 +00001949SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1951) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001952 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953}
1954
1955FPTruncInst::FPTruncInst(
1956 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1957) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001958 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959}
1960
1961FPTruncInst::FPTruncInst(
1962 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1963) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001964 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965}
1966
1967FPExtInst::FPExtInst(
1968 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1969) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001970 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971}
1972
1973FPExtInst::FPExtInst(
1974 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1975) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001976 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977}
1978
1979UIToFPInst::UIToFPInst(
1980 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1981) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001982 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001983}
1984
1985UIToFPInst::UIToFPInst(
1986 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1987) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001988 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989}
1990
1991SIToFPInst::SIToFPInst(
1992 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1993) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001994 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995}
1996
1997SIToFPInst::SIToFPInst(
1998 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1999) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002000 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001}
2002
2003FPToUIInst::FPToUIInst(
2004 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2005) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002006 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007}
2008
2009FPToUIInst::FPToUIInst(
2010 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2011) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002012 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013}
2014
2015FPToSIInst::FPToSIInst(
2016 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2017) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002018 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019}
2020
2021FPToSIInst::FPToSIInst(
2022 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2023) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002024 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025}
2026
2027PtrToIntInst::PtrToIntInst(
2028 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2029) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002030 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031}
2032
2033PtrToIntInst::PtrToIntInst(
2034 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2035) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002036 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037}
2038
2039IntToPtrInst::IntToPtrInst(
2040 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2041) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002042 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043}
2044
2045IntToPtrInst::IntToPtrInst(
2046 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2047) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002048 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049}
2050
2051BitCastInst::BitCastInst(
2052 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2053) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002054 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055}
2056
2057BitCastInst::BitCastInst(
2058 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2059) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002060 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002062
2063//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002064// CmpInst Classes
2065//===----------------------------------------------------------------------===//
2066
2067CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2068 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002069 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002070 Ops[0].init(LHS, this);
2071 Ops[1].init(RHS, this);
2072 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002073 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002074 if (op == Instruction::ICmp) {
2075 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2076 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2077 "Invalid ICmp predicate value");
2078 const Type* Op0Ty = getOperand(0)->getType();
2079 const Type* Op1Ty = getOperand(1)->getType();
2080 assert(Op0Ty == Op1Ty &&
2081 "Both operands to ICmp instruction are not of the same type!");
2082 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002083 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002084 "Invalid operand types for ICmp instruction");
2085 return;
2086 }
2087 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2088 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2089 "Invalid FCmp predicate value");
2090 const Type* Op0Ty = getOperand(0)->getType();
2091 const Type* Op1Ty = getOperand(1)->getType();
2092 assert(Op0Ty == Op1Ty &&
2093 "Both operands to FCmp instruction are not of the same type!");
2094 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002095 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002096 "Invalid operand types for FCmp instruction");
2097}
2098
2099CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2100 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002101 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002102 Ops[0].init(LHS, this);
2103 Ops[1].init(RHS, this);
2104 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002105 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002106 if (op == Instruction::ICmp) {
2107 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2108 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2109 "Invalid ICmp predicate value");
2110
2111 const Type* Op0Ty = getOperand(0)->getType();
2112 const Type* Op1Ty = getOperand(1)->getType();
2113 assert(Op0Ty == Op1Ty &&
2114 "Both operands to ICmp instruction are not of the same type!");
2115 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002116 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002117 "Invalid operand types for ICmp instruction");
2118 return;
2119 }
2120 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2121 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2122 "Invalid FCmp predicate value");
2123 const Type* Op0Ty = getOperand(0)->getType();
2124 const Type* Op1Ty = getOperand(1)->getType();
2125 assert(Op0Ty == Op1Ty &&
2126 "Both operands to FCmp instruction are not of the same type!");
2127 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002128 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002129 "Invalid operand types for FCmp instruction");
2130}
2131
2132CmpInst *
2133CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2134 const std::string &Name, Instruction *InsertBefore) {
2135 if (Op == Instruction::ICmp) {
2136 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2137 InsertBefore);
2138 }
2139 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2140 InsertBefore);
2141}
2142
2143CmpInst *
2144CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2145 const std::string &Name, BasicBlock *InsertAtEnd) {
2146 if (Op == Instruction::ICmp) {
2147 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2148 InsertAtEnd);
2149 }
2150 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2151 InsertAtEnd);
2152}
2153
2154void CmpInst::swapOperands() {
2155 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2156 IC->swapOperands();
2157 else
2158 cast<FCmpInst>(this)->swapOperands();
2159}
2160
2161bool CmpInst::isCommutative() {
2162 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2163 return IC->isCommutative();
2164 return cast<FCmpInst>(this)->isCommutative();
2165}
2166
2167bool CmpInst::isEquality() {
2168 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2169 return IC->isEquality();
2170 return cast<FCmpInst>(this)->isEquality();
2171}
2172
2173
2174ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2175 switch (pred) {
2176 default:
2177 assert(!"Unknown icmp predicate!");
2178 case ICMP_EQ: return ICMP_NE;
2179 case ICMP_NE: return ICMP_EQ;
2180 case ICMP_UGT: return ICMP_ULE;
2181 case ICMP_ULT: return ICMP_UGE;
2182 case ICMP_UGE: return ICMP_ULT;
2183 case ICMP_ULE: return ICMP_UGT;
2184 case ICMP_SGT: return ICMP_SLE;
2185 case ICMP_SLT: return ICMP_SGE;
2186 case ICMP_SGE: return ICMP_SLT;
2187 case ICMP_SLE: return ICMP_SGT;
2188 }
2189}
2190
2191ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2192 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002193 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002194 case ICMP_EQ: case ICMP_NE:
2195 return pred;
2196 case ICMP_SGT: return ICMP_SLT;
2197 case ICMP_SLT: return ICMP_SGT;
2198 case ICMP_SGE: return ICMP_SLE;
2199 case ICMP_SLE: return ICMP_SGE;
2200 case ICMP_UGT: return ICMP_ULT;
2201 case ICMP_ULT: return ICMP_UGT;
2202 case ICMP_UGE: return ICMP_ULE;
2203 case ICMP_ULE: return ICMP_UGE;
2204 }
2205}
2206
Reid Spencer266e42b2006-12-23 06:05:41 +00002207ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2208 switch (pred) {
2209 default: assert(! "Unknown icmp predicate!");
2210 case ICMP_EQ: case ICMP_NE:
2211 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2212 return pred;
2213 case ICMP_UGT: return ICMP_SGT;
2214 case ICMP_ULT: return ICMP_SLT;
2215 case ICMP_UGE: return ICMP_SGE;
2216 case ICMP_ULE: return ICMP_SLE;
2217 }
2218}
2219
2220bool ICmpInst::isSignedPredicate(Predicate pred) {
2221 switch (pred) {
2222 default: assert(! "Unknown icmp predicate!");
2223 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2224 return true;
2225 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2226 case ICMP_UGE: case ICMP_ULE:
2227 return false;
2228 }
2229}
2230
Reid Spencer0286bc12007-02-28 22:00:54 +00002231/// Initialize a set of values that all satisfy the condition with C.
2232///
2233ConstantRange
2234ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2235 APInt Lower(C);
2236 APInt Upper(C);
2237 uint32_t BitWidth = C.getBitWidth();
2238 switch (pred) {
2239 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2240 case ICmpInst::ICMP_EQ: Upper++; break;
2241 case ICmpInst::ICMP_NE: Lower++; break;
2242 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2243 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2244 case ICmpInst::ICMP_UGT:
2245 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2246 break;
2247 case ICmpInst::ICMP_SGT:
2248 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2249 break;
2250 case ICmpInst::ICMP_ULE:
2251 Lower = APInt::getMinValue(BitWidth); Upper++;
2252 break;
2253 case ICmpInst::ICMP_SLE:
2254 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2255 break;
2256 case ICmpInst::ICMP_UGE:
2257 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2258 break;
2259 case ICmpInst::ICMP_SGE:
2260 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2261 break;
2262 }
2263 return ConstantRange(Lower, Upper);
2264}
2265
Reid Spencerd9436b62006-11-20 01:22:35 +00002266FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2267 switch (pred) {
2268 default:
2269 assert(!"Unknown icmp predicate!");
2270 case FCMP_OEQ: return FCMP_UNE;
2271 case FCMP_ONE: return FCMP_UEQ;
2272 case FCMP_OGT: return FCMP_ULE;
2273 case FCMP_OLT: return FCMP_UGE;
2274 case FCMP_OGE: return FCMP_ULT;
2275 case FCMP_OLE: return FCMP_UGT;
2276 case FCMP_UEQ: return FCMP_ONE;
2277 case FCMP_UNE: return FCMP_OEQ;
2278 case FCMP_UGT: return FCMP_OLE;
2279 case FCMP_ULT: return FCMP_OGE;
2280 case FCMP_UGE: return FCMP_OLT;
2281 case FCMP_ULE: return FCMP_OGT;
2282 case FCMP_ORD: return FCMP_UNO;
2283 case FCMP_UNO: return FCMP_ORD;
2284 case FCMP_TRUE: return FCMP_FALSE;
2285 case FCMP_FALSE: return FCMP_TRUE;
2286 }
2287}
2288
2289FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2290 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002291 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002292 case FCMP_FALSE: case FCMP_TRUE:
2293 case FCMP_OEQ: case FCMP_ONE:
2294 case FCMP_UEQ: case FCMP_UNE:
2295 case FCMP_ORD: case FCMP_UNO:
2296 return pred;
2297 case FCMP_OGT: return FCMP_OLT;
2298 case FCMP_OLT: return FCMP_OGT;
2299 case FCMP_OGE: return FCMP_OLE;
2300 case FCMP_OLE: return FCMP_OGE;
2301 case FCMP_UGT: return FCMP_ULT;
2302 case FCMP_ULT: return FCMP_UGT;
2303 case FCMP_UGE: return FCMP_ULE;
2304 case FCMP_ULE: return FCMP_UGE;
2305 }
2306}
2307
Reid Spencer266e42b2006-12-23 06:05:41 +00002308bool CmpInst::isUnsigned(unsigned short predicate) {
2309 switch (predicate) {
2310 default: return false;
2311 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2312 case ICmpInst::ICMP_UGE: return true;
2313 }
2314}
2315
2316bool CmpInst::isSigned(unsigned short predicate){
2317 switch (predicate) {
2318 default: return false;
2319 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2320 case ICmpInst::ICMP_SGE: return true;
2321 }
2322}
2323
2324bool CmpInst::isOrdered(unsigned short predicate) {
2325 switch (predicate) {
2326 default: return false;
2327 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2328 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2329 case FCmpInst::FCMP_ORD: return true;
2330 }
2331}
2332
2333bool CmpInst::isUnordered(unsigned short predicate) {
2334 switch (predicate) {
2335 default: return false;
2336 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2337 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2338 case FCmpInst::FCMP_UNO: return true;
2339 }
2340}
2341
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002342//===----------------------------------------------------------------------===//
2343// SwitchInst Implementation
2344//===----------------------------------------------------------------------===//
2345
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002346void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002347 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002348 ReservedSpace = 2+NumCases*2;
2349 NumOperands = 2;
2350 OperandList = new Use[ReservedSpace];
2351
2352 OperandList[0].init(Value, this);
2353 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002354}
2355
Chris Lattner2195fc42007-02-24 00:55:48 +00002356/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2357/// switch on and a default destination. The number of additional cases can
2358/// be specified here to make memory allocation more efficient. This
2359/// constructor can also autoinsert before another instruction.
2360SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2361 Instruction *InsertBefore)
2362 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2363 init(Value, Default, NumCases);
2364}
2365
2366/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2367/// switch on and a default destination. The number of additional cases can
2368/// be specified here to make memory allocation more efficient. This
2369/// constructor also autoinserts at the end of the specified BasicBlock.
2370SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2371 BasicBlock *InsertAtEnd)
2372 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2373 init(Value, Default, NumCases);
2374}
2375
Misha Brukmanb1c93172005-04-21 23:48:37 +00002376SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002377 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2378 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002379 Use *OL = OperandList, *InOL = SI.OperandList;
2380 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2381 OL[i].init(InOL[i], this);
2382 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002383 }
2384}
2385
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002386SwitchInst::~SwitchInst() {
2387 delete [] OperandList;
2388}
2389
2390
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002391/// addCase - Add an entry to the switch instruction...
2392///
Chris Lattner47ac1872005-02-24 05:32:09 +00002393void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002394 unsigned OpNo = NumOperands;
2395 if (OpNo+2 > ReservedSpace)
2396 resizeOperands(0); // Get more space!
2397 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002398 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002399 NumOperands = OpNo+2;
2400 OperandList[OpNo].init(OnVal, this);
2401 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002402}
2403
2404/// removeCase - This method removes the specified successor from the switch
2405/// instruction. Note that this cannot be used to remove the default
2406/// destination (successor #0).
2407///
2408void SwitchInst::removeCase(unsigned idx) {
2409 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002410 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2411
2412 unsigned NumOps = getNumOperands();
2413 Use *OL = OperandList;
2414
2415 // Move everything after this operand down.
2416 //
2417 // FIXME: we could just swap with the end of the list, then erase. However,
2418 // client might not expect this to happen. The code as it is thrashes the
2419 // use/def lists, which is kinda lame.
2420 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2421 OL[i-2] = OL[i];
2422 OL[i-2+1] = OL[i+1];
2423 }
2424
2425 // Nuke the last value.
2426 OL[NumOps-2].set(0);
2427 OL[NumOps-2+1].set(0);
2428 NumOperands = NumOps-2;
2429}
2430
2431/// resizeOperands - resize operands - This adjusts the length of the operands
2432/// list according to the following behavior:
2433/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2434/// of operation. This grows the number of ops by 1.5 times.
2435/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2436/// 3. If NumOps == NumOperands, trim the reserved space.
2437///
2438void SwitchInst::resizeOperands(unsigned NumOps) {
2439 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002440 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002441 } else if (NumOps*2 > NumOperands) {
2442 // No resize needed.
2443 if (ReservedSpace >= NumOps) return;
2444 } else if (NumOps == NumOperands) {
2445 if (ReservedSpace == NumOps) return;
2446 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002447 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002448 }
2449
2450 ReservedSpace = NumOps;
2451 Use *NewOps = new Use[NumOps];
2452 Use *OldOps = OperandList;
2453 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2454 NewOps[i].init(OldOps[i], this);
2455 OldOps[i].set(0);
2456 }
2457 delete [] OldOps;
2458 OperandList = NewOps;
2459}
2460
2461
2462BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2463 return getSuccessor(idx);
2464}
2465unsigned SwitchInst::getNumSuccessorsV() const {
2466 return getNumSuccessors();
2467}
2468void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2469 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002470}
Chris Lattnerf22be932004-10-15 23:52:53 +00002471
2472
2473// Define these methods here so vtables don't get emitted into every translation
2474// unit that uses these classes.
2475
2476GetElementPtrInst *GetElementPtrInst::clone() const {
2477 return new GetElementPtrInst(*this);
2478}
2479
2480BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002481 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002482}
2483
Reid Spencerd9436b62006-11-20 01:22:35 +00002484CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002485 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002486}
2487
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2489AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2490FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2491LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2492StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2493CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2494CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2495CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2496CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2497CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2498CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2499CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2500CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2501CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2502CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2503CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2504CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2505CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2507VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2508
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002509ExtractElementInst *ExtractElementInst::clone() const {
2510 return new ExtractElementInst(*this);
2511}
2512InsertElementInst *InsertElementInst::clone() const {
2513 return new InsertElementInst(*this);
2514}
2515ShuffleVectorInst *ShuffleVectorInst::clone() const {
2516 return new ShuffleVectorInst(*this);
2517}
Chris Lattnerf22be932004-10-15 23:52:53 +00002518PHINode *PHINode::clone() const { return new PHINode(*this); }
2519ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2520BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2521SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2522InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2523UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002524UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}