blob: e02175585a7a42d7a0feb7816e03ed25d10e92a9 [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"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
Chris Lattner054ba2c2007-02-13 00:58:44 +0000199void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
200 NumOperands = NumParams+1;
201 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000206 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000207
Chris Lattner054ba2c2007-02-13 00:58:44 +0000208 assert((NumParams == FTy->getNumParams() ||
209 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000210 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000211 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000212 assert((i >= FTy->getNumParams() ||
213 FTy->getParamType(i) == Params[i]->getType()) &&
214 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000216 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217}
218
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
220 NumOperands = 3;
221 Use *OL = OperandList = new Use[3];
222 OL[0].init(Func, this);
223 OL[1].init(Actual1, this);
224 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000225
226 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000228 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000231 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000233 assert((0 >= FTy->getNumParams() ||
234 FTy->getParamType(0) == Actual1->getType()) &&
235 "Calling a function with a bad signature!");
236 assert((1 >= FTy->getNumParams() ||
237 FTy->getParamType(1) == Actual2->getType()) &&
238 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241void CallInst::init(Value *Func, Value *Actual) {
242 NumOperands = 2;
243 Use *OL = OperandList = new Use[2];
244 OL[0].init(Func, this);
245 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
247 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000249 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251 assert((FTy->getNumParams() == 1 ||
252 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000254 assert((0 == FTy->getNumParams() ||
255 FTy->getParamType(0) == Actual->getType()) &&
256 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257}
258
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259void CallInst::init(Value *Func) {
260 NumOperands = 1;
261 Use *OL = OperandList = new Use[1];
262 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000266 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Misha Brukmanb1c93172005-04-21 23:48:37 +0000271CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
272 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275 Instruction::Call, 0, 0, Name, InsertBefore) {
Chris Lattner054ba2c2007-02-13 00:58:44 +0000276 init(Func, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277}
278
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
280 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
282 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Chris Lattner054ba2c2007-02-13 00:58:44 +0000284 init(Func, &Params[0], Params.size());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
287CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
288 const std::string &Name, Instruction *InsertBefore)
289 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
290 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292 init(Func, Actual1, Actual2);
293}
294
295CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
296 const std::string &Name, BasicBlock *InsertAtEnd)
297 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
298 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000299 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300 init(Func, Actual1, Actual2);
301}
302
303CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
304 Instruction *InsertBefore)
305 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
306 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308 init(Func, Actual);
309}
310
311CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316 init(Func, Actual);
317}
318
319CallInst::CallInst(Value *Func, const std::string &Name,
320 Instruction *InsertBefore)
321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000323 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func);
325}
326
327CallInst::CallInst(Value *Func, const std::string &Name,
328 BasicBlock *InsertAtEnd)
329 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
330 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000331 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 init(Func);
333}
334
Misha Brukmanb1c93172005-04-21 23:48:37 +0000335CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000336 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
337 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000338 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339 Use *OL = OperandList;
340 Use *InOL = CI.OperandList;
341 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
342 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345
346//===----------------------------------------------------------------------===//
347// InvokeInst Implementation
348//===----------------------------------------------------------------------===//
349
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000350InvokeInst::~InvokeInst() {
351 delete [] OperandList;
352}
353
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 const std::vector<Value*> &Params) {
356 NumOperands = 3+Params.size();
357 Use *OL = OperandList = new Use[3+Params.size()];
358 OL[0].init(Fn, this);
359 OL[1].init(IfNormal, this);
360 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000361 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000363 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000364
365 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000366 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000368
Chris Lattner667a0562006-05-03 00:48:22 +0000369 for (unsigned i = 0, e = Params.size(); i != e; i++) {
370 assert((i >= FTy->getNumParams() ||
371 FTy->getParamType(i) == Params[i]->getType()) &&
372 "Invoking a function with a bad signature!");
373
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000374 OL[i+3].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000375 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376}
377
378InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
379 BasicBlock *IfException,
380 const std::vector<Value*> &Params,
381 const std::string &Name, Instruction *InsertBefore)
382 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
383 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000384 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385 init(Fn, IfNormal, IfException, Params);
386}
387
388InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
389 BasicBlock *IfException,
390 const std::vector<Value*> &Params,
391 const std::string &Name, BasicBlock *InsertAtEnd)
392 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
393 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000394 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395 init(Fn, IfNormal, IfException, Params);
396}
397
Misha Brukmanb1c93172005-04-21 23:48:37 +0000398InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 : TerminatorInst(II.getType(), Instruction::Invoke,
400 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000401 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 Use *OL = OperandList, *InOL = II.OperandList;
403 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
404 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405}
406
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000407BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
408 return getSuccessor(idx);
409}
410unsigned InvokeInst::getNumSuccessorsV() const {
411 return getNumSuccessors();
412}
413void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
414 return setSuccessor(idx, B);
415}
416
417
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418//===----------------------------------------------------------------------===//
419// ReturnInst Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000422void ReturnInst::init(Value *retVal) {
423 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000424 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000425 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000426 NumOperands = 1;
427 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000428 }
429}
430
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000431unsigned ReturnInst::getNumSuccessorsV() const {
432 return getNumSuccessors();
433}
434
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000435// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
436// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000437void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438 assert(0 && "ReturnInst has no successors!");
439}
440
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000441BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
442 assert(0 && "ReturnInst has no successors!");
443 abort();
444 return 0;
445}
446
447
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000448//===----------------------------------------------------------------------===//
449// UnwindInst Implementation
450//===----------------------------------------------------------------------===//
451
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000452unsigned UnwindInst::getNumSuccessorsV() const {
453 return getNumSuccessors();
454}
455
456void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000457 assert(0 && "UnwindInst has no successors!");
458}
459
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000460BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
461 assert(0 && "UnwindInst has no successors!");
462 abort();
463 return 0;
464}
465
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000466//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000467// UnreachableInst Implementation
468//===----------------------------------------------------------------------===//
469
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000470unsigned UnreachableInst::getNumSuccessorsV() const {
471 return getNumSuccessors();
472}
473
474void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
475 assert(0 && "UnwindInst has no successors!");
476}
477
478BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
479 assert(0 && "UnwindInst has no successors!");
480 abort();
481 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000482}
483
484//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000485// BranchInst Implementation
486//===----------------------------------------------------------------------===//
487
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000488void BranchInst::AssertOK() {
489 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000490 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000491 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000492}
493
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000494BranchInst::BranchInst(const BranchInst &BI) :
495 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
496 OperandList[0].init(BI.getOperand(0), this);
497 if (BI.getNumOperands() != 1) {
498 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
499 OperandList[1].init(BI.getOperand(1), this);
500 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501 }
502}
503
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000504BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
505 return getSuccessor(idx);
506}
507unsigned BranchInst::getNumSuccessorsV() const {
508 return getNumSuccessors();
509}
510void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
511 setSuccessor(idx, B);
512}
513
514
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000515//===----------------------------------------------------------------------===//
516// AllocationInst Implementation
517//===----------------------------------------------------------------------===//
518
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000519static Value *getAISize(Value *Amt) {
520 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000521 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000522 else {
523 assert(!isa<BasicBlock>(Amt) &&
524 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000525 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000526 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000527 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000528 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000529}
530
Misha Brukmanb1c93172005-04-21 23:48:37 +0000531AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000532 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000533 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000534 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000535 Name, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000536 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538}
539
Misha Brukmanb1c93172005-04-21 23:48:37 +0000540AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000541 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000544 Name, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000545 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000547}
548
Chris Lattner1c12a882006-06-21 16:53:47 +0000549// Out of line virtual method, so the vtable, etc has a home.
550AllocationInst::~AllocationInst() {
551}
552
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000554 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
555 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000557}
558
559const Type *AllocationInst::getAllocatedType() const {
560 return getType()->getElementType();
561}
562
563AllocaInst::AllocaInst(const AllocaInst &AI)
564 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000565 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
568MallocInst::MallocInst(const MallocInst &MI)
569 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000570 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000571}
572
573//===----------------------------------------------------------------------===//
574// FreeInst Implementation
575//===----------------------------------------------------------------------===//
576
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000577void FreeInst::AssertOK() {
578 assert(isa<PointerType>(getOperand(0)->getType()) &&
579 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580}
581
582FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
584 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
587FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000588 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
589 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000590}
591
592
593//===----------------------------------------------------------------------===//
594// LoadInst Implementation
595//===----------------------------------------------------------------------===//
596
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000598 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600}
601
602LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000604 Load, Ptr, Name, InsertBef) {
605 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
609LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000610 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000611 Load, Ptr, Name, InsertAE) {
612 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614}
615
616LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
617 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000619 Load, Ptr, Name, InsertBef) {
620 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622}
623
624LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
625 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000627 Load, Ptr, Name, InsertAE) {
628 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000629 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000630}
631
632
633//===----------------------------------------------------------------------===//
634// StoreInst Implementation
635//===----------------------------------------------------------------------===//
636
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637void StoreInst::AssertOK() {
638 assert(isa<PointerType>(getOperand(1)->getType()) &&
639 "Ptr must have pointer type!");
640 assert(getOperand(0)->getType() ==
641 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000642 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645
646StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000647 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648 Ops[0].init(val, this);
649 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000650 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651 AssertOK();
652}
653
654StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000655 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 Ops[0].init(val, this);
657 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000658 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 AssertOK();
660}
661
Misha Brukmanb1c93172005-04-21 23:48:37 +0000662StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000664 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 Ops[0].init(val, this);
666 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000667 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000668 AssertOK();
669}
670
Misha Brukmanb1c93172005-04-21 23:48:37 +0000671StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000673 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000674 Ops[0].init(val, this);
675 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000676 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678}
679
680//===----------------------------------------------------------------------===//
681// GetElementPtrInst Implementation
682//===----------------------------------------------------------------------===//
683
684// checkType - Simple wrapper function to give a better assertion failure
685// message on bad indexes for a gep instruction.
686//
687static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000688 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689 return Ty;
690}
691
Chris Lattner79807c3d2007-01-31 19:47:18 +0000692void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
693 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694 Use *OL = OperandList = new Use[NumOperands];
695 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000696
Chris Lattner79807c3d2007-01-31 19:47:18 +0000697 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699}
700
701void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702 NumOperands = 3;
703 Use *OL = OperandList = new Use[3];
704 OL[0].init(Ptr, this);
705 OL[1].init(Idx0, this);
706 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
Chris Lattner82981202005-05-03 05:43:30 +0000709void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
710 NumOperands = 2;
711 Use *OL = OperandList = new Use[2];
712 OL[0].init(Ptr, this);
713 OL[1].init(Idx, this);
714}
715
Chris Lattner79807c3d2007-01-31 19:47:18 +0000716
717GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
718 unsigned NumIdx,
719 const std::string &Name, Instruction *InBe)
720: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000721 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000722 GetElementPtr, 0, 0, Name, InBe) {
723 init(Ptr, Idx, NumIdx);
724}
725
726GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
727 unsigned NumIdx,
728 const std::string &Name, BasicBlock *IAE)
729: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000730 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000731 GetElementPtr, 0, 0, Name, IAE) {
732 init(Ptr, Idx, NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Chris Lattner82981202005-05-03 05:43:30 +0000735GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
736 const std::string &Name, Instruction *InBe)
Reid Spencerdee14b52007-01-31 22:30:26 +0000737 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
738 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000739 GetElementPtr, 0, 0, Name, InBe) {
740 init(Ptr, Idx);
741}
742
743GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
744 const std::string &Name, BasicBlock *IAE)
Reid Spencerdee14b52007-01-31 22:30:26 +0000745 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
746 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000747 GetElementPtr, 0, 0, Name, IAE) {
748 init(Ptr, Idx);
749}
750
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
752 const std::string &Name, Instruction *InBe)
753 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
754 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000755 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756 init(Ptr, Idx0, Idx1);
757}
758
759GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000760 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000761 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
762 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000764 init(Ptr, Idx0, Idx1);
765}
766
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767GetElementPtrInst::~GetElementPtrInst() {
768 delete[] OperandList;
769}
770
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771// getIndexedType - Returns the type of the element that would be loaded with
772// a load instruction with the specified parameters.
773//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000774// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775// pointer type.
776//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000777const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000778 Value* const *Idxs,
779 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780 bool AllowCompositeLeaf) {
781 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
782
783 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000784 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785 if (AllowCompositeLeaf ||
786 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
787 return cast<PointerType>(Ptr)->getElementType();
788 else
789 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000790
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791 unsigned CurIdx = 0;
792 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000793 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
795 return 0; // Can't load a whole structure or array!?!?
796 }
797
Chris Lattner302116a2007-01-31 04:40:28 +0000798 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799 if (isa<PointerType>(CT) && CurIdx != 1)
800 return 0; // Can only index into pointer types at the first index!
801 if (!CT->indexValid(Index)) return 0;
802 Ptr = CT->getTypeAtIndex(Index);
803
804 // If the new type forwards to another type, then it is in the middle
805 // of being refined to another type (and hence, may have dropped all
806 // references to what it was using before). So, use the new forwarded
807 // type.
808 if (const Type * Ty = Ptr->getForwardedType()) {
809 Ptr = Ty;
810 }
811 }
Chris Lattner302116a2007-01-31 04:40:28 +0000812 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
Misha Brukmanb1c93172005-04-21 23:48:37 +0000815const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816 Value *Idx0, Value *Idx1,
817 bool AllowCompositeLeaf) {
818 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
819 if (!PTy) return 0; // Type isn't a pointer type!
820
821 // Check the pointer index.
822 if (!PTy->indexValid(Idx0)) return 0;
823
824 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
825 if (!CT || !CT->indexValid(Idx1)) return 0;
826
827 const Type *ElTy = CT->getTypeAtIndex(Idx1);
828 if (AllowCompositeLeaf || ElTy->isFirstClassType())
829 return ElTy;
830 return 0;
831}
832
Chris Lattner82981202005-05-03 05:43:30 +0000833const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
834 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
835 if (!PTy) return 0; // Type isn't a pointer type!
836
837 // Check the pointer index.
838 if (!PTy->indexValid(Idx)) return 0;
839
Chris Lattnerc2233332005-05-03 16:44:45 +0000840 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000841}
842
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000843//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000844// ExtractElementInst Implementation
845//===----------------------------------------------------------------------===//
846
847ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000848 const std::string &Name,
849 Instruction *InsertBef)
Robert Bocchino23004482006-01-10 19:05:34 +0000850 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
851 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000852 assert(isValidOperands(Val, Index) &&
853 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000854 Ops[0].init(Val, this);
855 Ops[1].init(Index, this);
856}
857
Chris Lattner65511ff2006-10-05 06:24:58 +0000858ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
859 const std::string &Name,
860 Instruction *InsertBef)
861 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
862 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000863 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000864 assert(isValidOperands(Val, Index) &&
865 "Invalid extractelement instruction operands!");
866 Ops[0].init(Val, this);
867 Ops[1].init(Index, this);
868}
869
870
Robert Bocchino23004482006-01-10 19:05:34 +0000871ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000872 const std::string &Name,
873 BasicBlock *InsertAE)
Robert Bocchino23004482006-01-10 19:05:34 +0000874 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
875 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000876 assert(isValidOperands(Val, Index) &&
877 "Invalid extractelement instruction operands!");
878
Robert Bocchino23004482006-01-10 19:05:34 +0000879 Ops[0].init(Val, this);
880 Ops[1].init(Index, this);
881}
882
Chris Lattner65511ff2006-10-05 06:24:58 +0000883ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
884 const std::string &Name,
885 BasicBlock *InsertAE)
886 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
887 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000888 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000889 assert(isValidOperands(Val, Index) &&
890 "Invalid extractelement instruction operands!");
891
892 Ops[0].init(Val, this);
893 Ops[1].init(Index, this);
894}
895
896
Chris Lattner54865b32006-04-08 04:05:48 +0000897bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000898 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000899 return false;
900 return true;
901}
902
903
Robert Bocchino23004482006-01-10 19:05:34 +0000904//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000905// InsertElementInst Implementation
906//===----------------------------------------------------------------------===//
907
Chris Lattner0875d942006-04-14 22:20:32 +0000908InsertElementInst::InsertElementInst(const InsertElementInst &IE)
909 : Instruction(IE.getType(), InsertElement, Ops, 3) {
910 Ops[0].init(IE.Ops[0], this);
911 Ops[1].init(IE.Ops[1], this);
912 Ops[2].init(IE.Ops[2], this);
913}
Chris Lattner54865b32006-04-08 04:05:48 +0000914InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000915 const std::string &Name,
916 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000917 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
918 assert(isValidOperands(Vec, Elt, Index) &&
919 "Invalid insertelement instruction operands!");
920 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000921 Ops[1].init(Elt, this);
922 Ops[2].init(Index, this);
923}
924
Chris Lattner65511ff2006-10-05 06:24:58 +0000925InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
926 const std::string &Name,
927 Instruction *InsertBef)
928 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000929 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000930 assert(isValidOperands(Vec, Elt, Index) &&
931 "Invalid insertelement instruction operands!");
932 Ops[0].init(Vec, this);
933 Ops[1].init(Elt, this);
934 Ops[2].init(Index, this);
935}
936
937
Chris Lattner54865b32006-04-08 04:05:48 +0000938InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000939 const std::string &Name,
940 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000941 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
942 assert(isValidOperands(Vec, Elt, Index) &&
943 "Invalid insertelement instruction operands!");
944
945 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000946 Ops[1].init(Elt, this);
947 Ops[2].init(Index, this);
948}
949
Chris Lattner65511ff2006-10-05 06:24:58 +0000950InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
951 const std::string &Name,
952 BasicBlock *InsertAE)
953: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000954 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000955 assert(isValidOperands(Vec, Elt, Index) &&
956 "Invalid insertelement instruction operands!");
957
958 Ops[0].init(Vec, this);
959 Ops[1].init(Elt, this);
960 Ops[2].init(Index, this);
961}
962
Chris Lattner54865b32006-04-08 04:05:48 +0000963bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
964 const Value *Index) {
965 if (!isa<PackedType>(Vec->getType()))
966 return false; // First operand of insertelement must be packed type.
967
968 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
969 return false;// Second operand of insertelement must be packed element type.
970
Reid Spencer8d9336d2006-12-31 05:26:44 +0000971 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000972 return false; // Third operand of insertelement must be uint.
973 return true;
974}
975
976
Robert Bocchinoca27f032006-01-17 20:07:22 +0000977//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000978// ShuffleVectorInst Implementation
979//===----------------------------------------------------------------------===//
980
Chris Lattner0875d942006-04-14 22:20:32 +0000981ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
982 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
983 Ops[0].init(SV.Ops[0], this);
984 Ops[1].init(SV.Ops[1], this);
985 Ops[2].init(SV.Ops[2], this);
986}
987
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000988ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
989 const std::string &Name,
990 Instruction *InsertBefore)
991 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
992 assert(isValidOperands(V1, V2, Mask) &&
993 "Invalid shuffle vector instruction operands!");
994 Ops[0].init(V1, this);
995 Ops[1].init(V2, this);
996 Ops[2].init(Mask, this);
997}
998
999ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1000 const std::string &Name,
1001 BasicBlock *InsertAtEnd)
1002 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
1003 assert(isValidOperands(V1, V2, Mask) &&
1004 "Invalid shuffle vector instruction operands!");
1005
1006 Ops[0].init(V1, this);
1007 Ops[1].init(V2, this);
1008 Ops[2].init(Mask, this);
1009}
1010
1011bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1012 const Value *Mask) {
1013 if (!isa<PackedType>(V1->getType())) return false;
1014 if (V1->getType() != V2->getType()) return false;
1015 if (!isa<PackedType>(Mask->getType()) ||
Reid Spencer8d9336d2006-12-31 05:26:44 +00001016 cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty ||
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001017 cast<PackedType>(Mask->getType())->getNumElements() !=
1018 cast<PackedType>(V1->getType())->getNumElements())
1019 return false;
1020 return true;
1021}
1022
1023
1024//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001025// BinaryOperator Class
1026//===----------------------------------------------------------------------===//
1027
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001028void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001030 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001031 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032 assert(LHS->getType() == RHS->getType() &&
1033 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034#ifndef NDEBUG
1035 switch (iType) {
1036 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001037 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001039 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001040 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001041 isa<PackedType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001042 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001044 case UDiv:
1045 case SDiv:
1046 assert(getType() == LHS->getType() &&
1047 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001048 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1049 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001050 "Incorrect operand type (not integer) for S/UDIV");
1051 break;
1052 case FDiv:
1053 assert(getType() == LHS->getType() &&
1054 "Arithmetic operation should return same type as operands!");
1055 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1056 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1057 && "Incorrect operand type (not floating point) for FDIV");
1058 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001059 case URem:
1060 case SRem:
1061 assert(getType() == LHS->getType() &&
1062 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001063 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1064 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001065 "Incorrect operand type (not integer) for S/UREM");
1066 break;
1067 case FRem:
1068 assert(getType() == LHS->getType() &&
1069 "Arithmetic operation should return same type as operands!");
1070 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1071 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1072 && "Incorrect operand type (not floating point) for FREM");
1073 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001074 case Shl:
1075 case LShr:
1076 case AShr:
1077 assert(getType() == LHS->getType() &&
1078 "Shift operation should return same type as operands!");
1079 assert(getType()->isInteger() &&
1080 "Shift operation requires integer operands");
1081 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001082 case And: case Or:
1083 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001084 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001086 assert((getType()->isInteger() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001087 (isa<PackedType>(getType()) &&
Chris Lattner03c49532007-01-15 02:27:26 +00001088 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001089 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001091 default:
1092 break;
1093 }
1094#endif
1095}
1096
1097BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001098 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001099 Instruction *InsertBefore) {
1100 assert(S1->getType() == S2->getType() &&
1101 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001102 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103}
1104
1105BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001106 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001107 BasicBlock *InsertAtEnd) {
1108 BinaryOperator *Res = create(Op, S1, S2, Name);
1109 InsertAtEnd->getInstList().push_back(Res);
1110 return Res;
1111}
1112
1113BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1114 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001115 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1116 return new BinaryOperator(Instruction::Sub,
1117 zero, Op,
1118 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001119}
1120
1121BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1122 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001123 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1124 return new BinaryOperator(Instruction::Sub,
1125 zero, Op,
1126 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001127}
1128
1129BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1130 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001131 Constant *C;
1132 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001133 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001134 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1135 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001136 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001137 }
1138
1139 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001140 Op->getType(), Name, InsertBefore);
1141}
1142
1143BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1144 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001145 Constant *AllOnes;
1146 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1147 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001148 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001149 AllOnes =
1150 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1151 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001152 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001153 }
1154
1155 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001156 Op->getType(), Name, InsertAtEnd);
1157}
1158
1159
1160// isConstantAllOnes - Helper function for several functions below
1161static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001162 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163}
1164
1165bool BinaryOperator::isNeg(const Value *V) {
1166 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1167 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001168 return Bop->getOperand(0) ==
1169 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001170 return false;
1171}
1172
1173bool BinaryOperator::isNot(const Value *V) {
1174 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1175 return (Bop->getOpcode() == Instruction::Xor &&
1176 (isConstantAllOnes(Bop->getOperand(1)) ||
1177 isConstantAllOnes(Bop->getOperand(0))));
1178 return false;
1179}
1180
Chris Lattner2c7d1772005-04-24 07:28:37 +00001181Value *BinaryOperator::getNegArgument(Value *BinOp) {
1182 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1183 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184}
1185
Chris Lattner2c7d1772005-04-24 07:28:37 +00001186const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1187 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001188}
1189
Chris Lattner2c7d1772005-04-24 07:28:37 +00001190Value *BinaryOperator::getNotArgument(Value *BinOp) {
1191 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1192 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1193 Value *Op0 = BO->getOperand(0);
1194 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001195 if (isConstantAllOnes(Op0)) return Op1;
1196
1197 assert(isConstantAllOnes(Op1));
1198 return Op0;
1199}
1200
Chris Lattner2c7d1772005-04-24 07:28:37 +00001201const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1202 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001203}
1204
1205
1206// swapOperands - Exchange the two operands to this instruction. This
1207// instruction is safe to use on any binary instruction and does not
1208// modify the semantics of the instruction. If the instruction is
1209// order dependent (SetLT f.e.) the opcode is changed.
1210//
1211bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001212 if (!isCommutative())
1213 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215 return false;
1216}
1217
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001218//===----------------------------------------------------------------------===//
1219// CastInst Class
1220//===----------------------------------------------------------------------===//
1221
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001222// Just determine if this cast only deals with integral->integral conversion.
1223bool CastInst::isIntegerCast() const {
1224 switch (getOpcode()) {
1225 default: return false;
1226 case Instruction::ZExt:
1227 case Instruction::SExt:
1228 case Instruction::Trunc:
1229 return true;
1230 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001231 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001232 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001233}
1234
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001235bool CastInst::isLosslessCast() const {
1236 // Only BitCast can be lossless, exit fast if we're not BitCast
1237 if (getOpcode() != Instruction::BitCast)
1238 return false;
1239
1240 // Identity cast is always lossless
1241 const Type* SrcTy = getOperand(0)->getType();
1242 const Type* DstTy = getType();
1243 if (SrcTy == DstTy)
1244 return true;
1245
Reid Spencer8d9336d2006-12-31 05:26:44 +00001246 // Pointer to pointer is always lossless.
1247 if (isa<PointerType>(SrcTy))
1248 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001249 return false; // Other types have no identity values
1250}
1251
1252/// This function determines if the CastInst does not require any bits to be
1253/// changed in order to effect the cast. Essentially, it identifies cases where
1254/// no code gen is necessary for the cast, hence the name no-op cast. For
1255/// example, the following are all no-op casts:
1256/// # bitcast uint %X, int
1257/// # bitcast uint* %x, sbyte*
1258/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1259/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1260/// @brief Determine if a cast is a no-op.
1261bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1262 switch (getOpcode()) {
1263 default:
1264 assert(!"Invalid CastOp");
1265 case Instruction::Trunc:
1266 case Instruction::ZExt:
1267 case Instruction::SExt:
1268 case Instruction::FPTrunc:
1269 case Instruction::FPExt:
1270 case Instruction::UIToFP:
1271 case Instruction::SIToFP:
1272 case Instruction::FPToUI:
1273 case Instruction::FPToSI:
1274 return false; // These always modify bits
1275 case Instruction::BitCast:
1276 return true; // BitCast never modifies bits.
1277 case Instruction::PtrToInt:
1278 return IntPtrTy->getPrimitiveSizeInBits() ==
1279 getType()->getPrimitiveSizeInBits();
1280 case Instruction::IntToPtr:
1281 return IntPtrTy->getPrimitiveSizeInBits() ==
1282 getOperand(0)->getType()->getPrimitiveSizeInBits();
1283 }
1284}
1285
1286/// This function determines if a pair of casts can be eliminated and what
1287/// opcode should be used in the elimination. This assumes that there are two
1288/// instructions like this:
1289/// * %F = firstOpcode SrcTy %x to MidTy
1290/// * %S = secondOpcode MidTy %F to DstTy
1291/// The function returns a resultOpcode so these two casts can be replaced with:
1292/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1293/// If no such cast is permited, the function returns 0.
1294unsigned CastInst::isEliminableCastPair(
1295 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1296 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1297{
1298 // Define the 144 possibilities for these two cast instructions. The values
1299 // in this matrix determine what to do in a given situation and select the
1300 // case in the switch below. The rows correspond to firstOp, the columns
1301 // correspond to secondOp. In looking at the table below, keep in mind
1302 // the following cast properties:
1303 //
1304 // Size Compare Source Destination
1305 // Operator Src ? Size Type Sign Type Sign
1306 // -------- ------------ ------------------- ---------------------
1307 // TRUNC > Integer Any Integral Any
1308 // ZEXT < Integral Unsigned Integer Any
1309 // SEXT < Integral Signed Integer Any
1310 // FPTOUI n/a FloatPt n/a Integral Unsigned
1311 // FPTOSI n/a FloatPt n/a Integral Signed
1312 // UITOFP n/a Integral Unsigned FloatPt n/a
1313 // SITOFP n/a Integral Signed FloatPt n/a
1314 // FPTRUNC > FloatPt n/a FloatPt n/a
1315 // FPEXT < FloatPt n/a FloatPt n/a
1316 // PTRTOINT n/a Pointer n/a Integral Unsigned
1317 // INTTOPTR n/a Integral Unsigned Pointer n/a
1318 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001319 //
1320 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1321 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1322 // into "fptoui double to ulong", but this loses information about the range
1323 // of the produced value (we no longer know the top-part is all zeros).
1324 // Further this conversion is often much more expensive for typical hardware,
1325 // and causes issues when building libgcc. We disallow fptosi+sext for the
1326 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001327 const unsigned numCastOps =
1328 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1329 static const uint8_t CastResults[numCastOps][numCastOps] = {
1330 // T F F U S F F P I B -+
1331 // R Z S P P I I T P 2 N T |
1332 // U E E 2 2 2 2 R E I T C +- secondOp
1333 // N X X U S F F N X N 2 V |
1334 // C T T I I P P C T T P T -+
1335 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1336 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1337 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001338 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1339 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001340 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1341 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1342 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1343 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1344 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1345 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1346 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1347 };
1348
1349 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1350 [secondOp-Instruction::CastOpsBegin];
1351 switch (ElimCase) {
1352 case 0:
1353 // categorically disallowed
1354 return 0;
1355 case 1:
1356 // allowed, use first cast's opcode
1357 return firstOp;
1358 case 2:
1359 // allowed, use second cast's opcode
1360 return secondOp;
1361 case 3:
1362 // no-op cast in second op implies firstOp as long as the DestTy
1363 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001364 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001365 return firstOp;
1366 return 0;
1367 case 4:
1368 // no-op cast in second op implies firstOp as long as the DestTy
1369 // is floating point
1370 if (DstTy->isFloatingPoint())
1371 return firstOp;
1372 return 0;
1373 case 5:
1374 // no-op cast in first op implies secondOp as long as the SrcTy
1375 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001376 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001377 return secondOp;
1378 return 0;
1379 case 6:
1380 // no-op cast in first op implies secondOp as long as the SrcTy
1381 // is a floating point
1382 if (SrcTy->isFloatingPoint())
1383 return secondOp;
1384 return 0;
1385 case 7: {
1386 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1387 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1388 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1389 if (MidSize >= PtrSize)
1390 return Instruction::BitCast;
1391 return 0;
1392 }
1393 case 8: {
1394 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1395 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1396 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1397 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1398 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1399 if (SrcSize == DstSize)
1400 return Instruction::BitCast;
1401 else if (SrcSize < DstSize)
1402 return firstOp;
1403 return secondOp;
1404 }
1405 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1406 return Instruction::ZExt;
1407 case 10:
1408 // fpext followed by ftrunc is allowed if the bit size returned to is
1409 // the same as the original, in which case its just a bitcast
1410 if (SrcTy == DstTy)
1411 return Instruction::BitCast;
1412 return 0; // If the types are not the same we can't eliminate it.
1413 case 11:
1414 // bitcast followed by ptrtoint is allowed as long as the bitcast
1415 // is a pointer to pointer cast.
1416 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1417 return secondOp;
1418 return 0;
1419 case 12:
1420 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1421 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1422 return firstOp;
1423 return 0;
1424 case 13: {
1425 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1426 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1427 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1428 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1429 if (SrcSize <= PtrSize && SrcSize == DstSize)
1430 return Instruction::BitCast;
1431 return 0;
1432 }
1433 case 99:
1434 // cast combination can't happen (error in input). This is for all cases
1435 // where the MidTy is not the same for the two cast instructions.
1436 assert(!"Invalid Cast Combination");
1437 return 0;
1438 default:
1439 assert(!"Error in CastResults table!!!");
1440 return 0;
1441 }
1442 return 0;
1443}
1444
1445CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1446 const std::string &Name, Instruction *InsertBefore) {
1447 // Construct and return the appropriate CastInst subclass
1448 switch (op) {
1449 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1450 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1451 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1452 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1453 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1454 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1455 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1456 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1457 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1458 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1459 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1460 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1461 default:
1462 assert(!"Invalid opcode provided");
1463 }
1464 return 0;
1465}
1466
1467CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1468 const std::string &Name, BasicBlock *InsertAtEnd) {
1469 // Construct and return the appropriate CastInst subclass
1470 switch (op) {
1471 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1472 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1473 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1474 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1475 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1476 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1477 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1478 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1479 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1480 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1481 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1482 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1483 default:
1484 assert(!"Invalid opcode provided");
1485 }
1486 return 0;
1487}
1488
Reid Spencer5c140882006-12-04 20:17:56 +00001489CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1490 const std::string &Name,
1491 Instruction *InsertBefore) {
1492 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1493 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1494 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1495}
1496
1497CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1498 const std::string &Name,
1499 BasicBlock *InsertAtEnd) {
1500 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1501 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1502 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1503}
1504
1505CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1506 const std::string &Name,
1507 Instruction *InsertBefore) {
1508 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1509 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1510 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1511}
1512
1513CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1514 const std::string &Name,
1515 BasicBlock *InsertAtEnd) {
1516 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1517 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1518 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1519}
1520
1521CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1522 const std::string &Name,
1523 Instruction *InsertBefore) {
1524 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1525 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1526 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1527}
1528
1529CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1530 const std::string &Name,
1531 BasicBlock *InsertAtEnd) {
1532 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1533 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1534 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1535}
1536
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001537CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1538 const std::string &Name,
1539 BasicBlock *InsertAtEnd) {
1540 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001541 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001542 "Invalid cast");
1543
Chris Lattner03c49532007-01-15 02:27:26 +00001544 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001545 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1546 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1547}
1548
1549/// @brief Create a BitCast or a PtrToInt cast instruction
1550CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1551 const std::string &Name,
1552 Instruction *InsertBefore) {
1553 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001554 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001555 "Invalid cast");
1556
Chris Lattner03c49532007-01-15 02:27:26 +00001557 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001558 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1559 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1560}
1561
Reid Spencer7e933472006-12-12 00:49:44 +00001562CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1563 bool isSigned, const std::string &Name,
1564 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001565 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001566 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1567 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1568 Instruction::CastOps opcode =
1569 (SrcBits == DstBits ? Instruction::BitCast :
1570 (SrcBits > DstBits ? Instruction::Trunc :
1571 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1572 return create(opcode, C, Ty, Name, InsertBefore);
1573}
1574
1575CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1576 bool isSigned, const std::string &Name,
1577 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001578 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001579 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1580 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1581 Instruction::CastOps opcode =
1582 (SrcBits == DstBits ? Instruction::BitCast :
1583 (SrcBits > DstBits ? Instruction::Trunc :
1584 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1585 return create(opcode, C, Ty, Name, InsertAtEnd);
1586}
1587
1588CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1589 const std::string &Name,
1590 Instruction *InsertBefore) {
1591 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1592 "Invalid cast");
1593 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1594 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1595 Instruction::CastOps opcode =
1596 (SrcBits == DstBits ? Instruction::BitCast :
1597 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1598 return create(opcode, C, Ty, Name, InsertBefore);
1599}
1600
1601CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1602 const std::string &Name,
1603 BasicBlock *InsertAtEnd) {
1604 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1605 "Invalid cast");
1606 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1607 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1608 Instruction::CastOps opcode =
1609 (SrcBits == DstBits ? Instruction::BitCast :
1610 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1611 return create(opcode, C, Ty, Name, InsertAtEnd);
1612}
1613
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001614// Provide a way to get a "cast" where the cast opcode is inferred from the
1615// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001616// logic in the castIsValid function below. This axiom should hold:
1617// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1618// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001619// casting opcode for the arguments passed to it.
1620Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001621CastInst::getCastOpcode(
1622 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001623 // Get the bit sizes, we'll need these
1624 const Type *SrcTy = Src->getType();
1625 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1626 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1627
1628 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001629 if (DestTy->isInteger()) { // Casting to integral
1630 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001631 if (DestBits < SrcBits)
1632 return Trunc; // int -> smaller int
1633 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001634 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001635 return SExt; // signed -> SEXT
1636 else
1637 return ZExt; // unsigned -> ZEXT
1638 } else {
1639 return BitCast; // Same size, No-op cast
1640 }
1641 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001642 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001643 return FPToSI; // FP -> sint
1644 else
1645 return FPToUI; // FP -> uint
1646 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1647 assert(DestBits == PTy->getBitWidth() &&
1648 "Casting packed to integer of different width");
1649 return BitCast; // Same size, no-op cast
1650 } else {
1651 assert(isa<PointerType>(SrcTy) &&
1652 "Casting from a value that is not first-class type");
1653 return PtrToInt; // ptr -> int
1654 }
1655 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001656 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001657 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001658 return SIToFP; // sint -> FP
1659 else
1660 return UIToFP; // uint -> FP
1661 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1662 if (DestBits < SrcBits) {
1663 return FPTrunc; // FP -> smaller FP
1664 } else if (DestBits > SrcBits) {
1665 return FPExt; // FP -> larger FP
1666 } else {
1667 return BitCast; // same size, no-op cast
1668 }
1669 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1670 assert(DestBits == PTy->getBitWidth() &&
1671 "Casting packed to floating point of different width");
1672 return BitCast; // same size, no-op cast
1673 } else {
1674 assert(0 && "Casting pointer or non-first class to float");
1675 }
1676 } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
1677 if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) {
1678 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1679 "Casting packed to packed of different widths");
1680 return BitCast; // packed -> packed
1681 } else if (DestPTy->getBitWidth() == SrcBits) {
1682 return BitCast; // float/int -> packed
1683 } else {
1684 assert(!"Illegal cast to packed (wrong type or size)");
1685 }
1686 } else if (isa<PointerType>(DestTy)) {
1687 if (isa<PointerType>(SrcTy)) {
1688 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001689 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001690 return IntToPtr; // int -> ptr
1691 } else {
1692 assert(!"Casting pointer to other than pointer or int");
1693 }
1694 } else {
1695 assert(!"Casting to type that is not first-class");
1696 }
1697
1698 // If we fall through to here we probably hit an assertion cast above
1699 // and assertions are not turned on. Anything we return is an error, so
1700 // BitCast is as good a choice as any.
1701 return BitCast;
1702}
1703
1704//===----------------------------------------------------------------------===//
1705// CastInst SubClass Constructors
1706//===----------------------------------------------------------------------===//
1707
1708/// Check that the construction parameters for a CastInst are correct. This
1709/// could be broken out into the separate constructors but it is useful to have
1710/// it in one place and to eliminate the redundant code for getting the sizes
1711/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001712bool
1713CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714
1715 // Check for type sanity on the arguments
1716 const Type *SrcTy = S->getType();
1717 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1718 return false;
1719
1720 // Get the size of the types in bits, we'll need this later
1721 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1722 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1723
1724 // Switch on the opcode provided
1725 switch (op) {
1726 default: return false; // This is an input error
1727 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001728 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001730 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001731 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001732 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001733 case Instruction::FPTrunc:
1734 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1735 SrcBitSize > DstBitSize;
1736 case Instruction::FPExt:
1737 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1738 SrcBitSize < DstBitSize;
1739 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001740 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001741 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001742 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001743 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001744 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001745 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001746 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001747 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001748 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001749 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001750 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001751 case Instruction::BitCast:
1752 // BitCast implies a no-op cast of type only. No bits change.
1753 // However, you can't cast pointers to anything but pointers.
1754 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1755 return false;
1756
1757 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1758 // these cases, the cast is okay if the source and destination bit widths
1759 // are identical.
1760 return SrcBitSize == DstBitSize;
1761 }
1762}
1763
1764TruncInst::TruncInst(
1765 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1766) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001767 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001768}
1769
1770TruncInst::TruncInst(
1771 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1772) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001773 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001774}
1775
1776ZExtInst::ZExtInst(
1777 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1778) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001779 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001780}
1781
1782ZExtInst::ZExtInst(
1783 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1784) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001785 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001786}
1787SExtInst::SExtInst(
1788 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1789) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001790 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001791}
1792
Jeff Cohencc08c832006-12-02 02:22:01 +00001793SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1795) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001796 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001797}
1798
1799FPTruncInst::FPTruncInst(
1800 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1801) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001802 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001803}
1804
1805FPTruncInst::FPTruncInst(
1806 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1807) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001808 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001809}
1810
1811FPExtInst::FPExtInst(
1812 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1813) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001814 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001815}
1816
1817FPExtInst::FPExtInst(
1818 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1819) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001820 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001821}
1822
1823UIToFPInst::UIToFPInst(
1824 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1825) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827}
1828
1829UIToFPInst::UIToFPInst(
1830 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1831) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833}
1834
1835SIToFPInst::SIToFPInst(
1836 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1837) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001838 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001839}
1840
1841SIToFPInst::SIToFPInst(
1842 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1843) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001844 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845}
1846
1847FPToUIInst::FPToUIInst(
1848 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1849) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001850 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001851}
1852
1853FPToUIInst::FPToUIInst(
1854 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1855) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001856 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001857}
1858
1859FPToSIInst::FPToSIInst(
1860 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1861) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001862 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001863}
1864
1865FPToSIInst::FPToSIInst(
1866 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1867) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001868 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001869}
1870
1871PtrToIntInst::PtrToIntInst(
1872 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1873) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001874 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875}
1876
1877PtrToIntInst::PtrToIntInst(
1878 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1879) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001880 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881}
1882
1883IntToPtrInst::IntToPtrInst(
1884 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1885) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001886 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887}
1888
1889IntToPtrInst::IntToPtrInst(
1890 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1891) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001892 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893}
1894
1895BitCastInst::BitCastInst(
1896 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1897) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001898 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899}
1900
1901BitCastInst::BitCastInst(
1902 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1903) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001904 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001906
1907//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001908// CmpInst Classes
1909//===----------------------------------------------------------------------===//
1910
1911CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1912 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001913 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001914 Ops[0].init(LHS, this);
1915 Ops[1].init(RHS, this);
1916 SubclassData = predicate;
1917 if (op == Instruction::ICmp) {
1918 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1919 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1920 "Invalid ICmp predicate value");
1921 const Type* Op0Ty = getOperand(0)->getType();
1922 const Type* Op1Ty = getOperand(1)->getType();
1923 assert(Op0Ty == Op1Ty &&
1924 "Both operands to ICmp instruction are not of the same type!");
1925 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001926 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001927 "Invalid operand types for ICmp instruction");
1928 return;
1929 }
1930 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1931 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1932 "Invalid FCmp predicate value");
1933 const Type* Op0Ty = getOperand(0)->getType();
1934 const Type* Op1Ty = getOperand(1)->getType();
1935 assert(Op0Ty == Op1Ty &&
1936 "Both operands to FCmp instruction are not of the same type!");
1937 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001938 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001939 "Invalid operand types for FCmp instruction");
1940}
1941
1942CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1943 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001944 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001945 Ops[0].init(LHS, this);
1946 Ops[1].init(RHS, this);
1947 SubclassData = predicate;
1948 if (op == Instruction::ICmp) {
1949 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1950 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1951 "Invalid ICmp predicate value");
1952
1953 const Type* Op0Ty = getOperand(0)->getType();
1954 const Type* Op1Ty = getOperand(1)->getType();
1955 assert(Op0Ty == Op1Ty &&
1956 "Both operands to ICmp instruction are not of the same type!");
1957 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001958 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001959 "Invalid operand types for ICmp instruction");
1960 return;
1961 }
1962 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1963 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1964 "Invalid FCmp predicate value");
1965 const Type* Op0Ty = getOperand(0)->getType();
1966 const Type* Op1Ty = getOperand(1)->getType();
1967 assert(Op0Ty == Op1Ty &&
1968 "Both operands to FCmp instruction are not of the same type!");
1969 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001970 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001971 "Invalid operand types for FCmp instruction");
1972}
1973
1974CmpInst *
1975CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1976 const std::string &Name, Instruction *InsertBefore) {
1977 if (Op == Instruction::ICmp) {
1978 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1979 InsertBefore);
1980 }
1981 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1982 InsertBefore);
1983}
1984
1985CmpInst *
1986CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1987 const std::string &Name, BasicBlock *InsertAtEnd) {
1988 if (Op == Instruction::ICmp) {
1989 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1990 InsertAtEnd);
1991 }
1992 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1993 InsertAtEnd);
1994}
1995
1996void CmpInst::swapOperands() {
1997 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
1998 IC->swapOperands();
1999 else
2000 cast<FCmpInst>(this)->swapOperands();
2001}
2002
2003bool CmpInst::isCommutative() {
2004 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2005 return IC->isCommutative();
2006 return cast<FCmpInst>(this)->isCommutative();
2007}
2008
2009bool CmpInst::isEquality() {
2010 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2011 return IC->isEquality();
2012 return cast<FCmpInst>(this)->isEquality();
2013}
2014
2015
2016ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2017 switch (pred) {
2018 default:
2019 assert(!"Unknown icmp predicate!");
2020 case ICMP_EQ: return ICMP_NE;
2021 case ICMP_NE: return ICMP_EQ;
2022 case ICMP_UGT: return ICMP_ULE;
2023 case ICMP_ULT: return ICMP_UGE;
2024 case ICMP_UGE: return ICMP_ULT;
2025 case ICMP_ULE: return ICMP_UGT;
2026 case ICMP_SGT: return ICMP_SLE;
2027 case ICMP_SLT: return ICMP_SGE;
2028 case ICMP_SGE: return ICMP_SLT;
2029 case ICMP_SLE: return ICMP_SGT;
2030 }
2031}
2032
2033ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2034 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002035 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002036 case ICMP_EQ: case ICMP_NE:
2037 return pred;
2038 case ICMP_SGT: return ICMP_SLT;
2039 case ICMP_SLT: return ICMP_SGT;
2040 case ICMP_SGE: return ICMP_SLE;
2041 case ICMP_SLE: return ICMP_SGE;
2042 case ICMP_UGT: return ICMP_ULT;
2043 case ICMP_ULT: return ICMP_UGT;
2044 case ICMP_UGE: return ICMP_ULE;
2045 case ICMP_ULE: return ICMP_UGE;
2046 }
2047}
2048
Reid Spencer266e42b2006-12-23 06:05:41 +00002049ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2050 switch (pred) {
2051 default: assert(! "Unknown icmp predicate!");
2052 case ICMP_EQ: case ICMP_NE:
2053 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2054 return pred;
2055 case ICMP_UGT: return ICMP_SGT;
2056 case ICMP_ULT: return ICMP_SLT;
2057 case ICMP_UGE: return ICMP_SGE;
2058 case ICMP_ULE: return ICMP_SLE;
2059 }
2060}
2061
2062bool ICmpInst::isSignedPredicate(Predicate pred) {
2063 switch (pred) {
2064 default: assert(! "Unknown icmp predicate!");
2065 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2066 return true;
2067 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2068 case ICMP_UGE: case ICMP_ULE:
2069 return false;
2070 }
2071}
2072
Reid Spencerd9436b62006-11-20 01:22:35 +00002073FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2074 switch (pred) {
2075 default:
2076 assert(!"Unknown icmp predicate!");
2077 case FCMP_OEQ: return FCMP_UNE;
2078 case FCMP_ONE: return FCMP_UEQ;
2079 case FCMP_OGT: return FCMP_ULE;
2080 case FCMP_OLT: return FCMP_UGE;
2081 case FCMP_OGE: return FCMP_ULT;
2082 case FCMP_OLE: return FCMP_UGT;
2083 case FCMP_UEQ: return FCMP_ONE;
2084 case FCMP_UNE: return FCMP_OEQ;
2085 case FCMP_UGT: return FCMP_OLE;
2086 case FCMP_ULT: return FCMP_OGE;
2087 case FCMP_UGE: return FCMP_OLT;
2088 case FCMP_ULE: return FCMP_OGT;
2089 case FCMP_ORD: return FCMP_UNO;
2090 case FCMP_UNO: return FCMP_ORD;
2091 case FCMP_TRUE: return FCMP_FALSE;
2092 case FCMP_FALSE: return FCMP_TRUE;
2093 }
2094}
2095
2096FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2097 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002098 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002099 case FCMP_FALSE: case FCMP_TRUE:
2100 case FCMP_OEQ: case FCMP_ONE:
2101 case FCMP_UEQ: case FCMP_UNE:
2102 case FCMP_ORD: case FCMP_UNO:
2103 return pred;
2104 case FCMP_OGT: return FCMP_OLT;
2105 case FCMP_OLT: return FCMP_OGT;
2106 case FCMP_OGE: return FCMP_OLE;
2107 case FCMP_OLE: return FCMP_OGE;
2108 case FCMP_UGT: return FCMP_ULT;
2109 case FCMP_ULT: return FCMP_UGT;
2110 case FCMP_UGE: return FCMP_ULE;
2111 case FCMP_ULE: return FCMP_UGE;
2112 }
2113}
2114
Reid Spencer266e42b2006-12-23 06:05:41 +00002115bool CmpInst::isUnsigned(unsigned short predicate) {
2116 switch (predicate) {
2117 default: return false;
2118 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2119 case ICmpInst::ICMP_UGE: return true;
2120 }
2121}
2122
2123bool CmpInst::isSigned(unsigned short predicate){
2124 switch (predicate) {
2125 default: return false;
2126 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2127 case ICmpInst::ICMP_SGE: return true;
2128 }
2129}
2130
2131bool CmpInst::isOrdered(unsigned short predicate) {
2132 switch (predicate) {
2133 default: return false;
2134 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2135 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2136 case FCmpInst::FCMP_ORD: return true;
2137 }
2138}
2139
2140bool CmpInst::isUnordered(unsigned short predicate) {
2141 switch (predicate) {
2142 default: return false;
2143 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2144 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2145 case FCmpInst::FCMP_UNO: return true;
2146 }
2147}
2148
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002149//===----------------------------------------------------------------------===//
2150// SwitchInst Implementation
2151//===----------------------------------------------------------------------===//
2152
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002153void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002154 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002155 ReservedSpace = 2+NumCases*2;
2156 NumOperands = 2;
2157 OperandList = new Use[ReservedSpace];
2158
2159 OperandList[0].init(Value, this);
2160 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002161}
2162
Misha Brukmanb1c93172005-04-21 23:48:37 +00002163SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002164 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2165 SI.getNumOperands()) {
2166 Use *OL = OperandList, *InOL = SI.OperandList;
2167 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2168 OL[i].init(InOL[i], this);
2169 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002170 }
2171}
2172
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002173SwitchInst::~SwitchInst() {
2174 delete [] OperandList;
2175}
2176
2177
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002178/// addCase - Add an entry to the switch instruction...
2179///
Chris Lattner47ac1872005-02-24 05:32:09 +00002180void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002181 unsigned OpNo = NumOperands;
2182 if (OpNo+2 > ReservedSpace)
2183 resizeOperands(0); // Get more space!
2184 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002185 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002186 NumOperands = OpNo+2;
2187 OperandList[OpNo].init(OnVal, this);
2188 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002189}
2190
2191/// removeCase - This method removes the specified successor from the switch
2192/// instruction. Note that this cannot be used to remove the default
2193/// destination (successor #0).
2194///
2195void SwitchInst::removeCase(unsigned idx) {
2196 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002197 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2198
2199 unsigned NumOps = getNumOperands();
2200 Use *OL = OperandList;
2201
2202 // Move everything after this operand down.
2203 //
2204 // FIXME: we could just swap with the end of the list, then erase. However,
2205 // client might not expect this to happen. The code as it is thrashes the
2206 // use/def lists, which is kinda lame.
2207 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2208 OL[i-2] = OL[i];
2209 OL[i-2+1] = OL[i+1];
2210 }
2211
2212 // Nuke the last value.
2213 OL[NumOps-2].set(0);
2214 OL[NumOps-2+1].set(0);
2215 NumOperands = NumOps-2;
2216}
2217
2218/// resizeOperands - resize operands - This adjusts the length of the operands
2219/// list according to the following behavior:
2220/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2221/// of operation. This grows the number of ops by 1.5 times.
2222/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2223/// 3. If NumOps == NumOperands, trim the reserved space.
2224///
2225void SwitchInst::resizeOperands(unsigned NumOps) {
2226 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002227 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002228 } else if (NumOps*2 > NumOperands) {
2229 // No resize needed.
2230 if (ReservedSpace >= NumOps) return;
2231 } else if (NumOps == NumOperands) {
2232 if (ReservedSpace == NumOps) return;
2233 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002234 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002235 }
2236
2237 ReservedSpace = NumOps;
2238 Use *NewOps = new Use[NumOps];
2239 Use *OldOps = OperandList;
2240 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2241 NewOps[i].init(OldOps[i], this);
2242 OldOps[i].set(0);
2243 }
2244 delete [] OldOps;
2245 OperandList = NewOps;
2246}
2247
2248
2249BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2250 return getSuccessor(idx);
2251}
2252unsigned SwitchInst::getNumSuccessorsV() const {
2253 return getNumSuccessors();
2254}
2255void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2256 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002257}
Chris Lattnerf22be932004-10-15 23:52:53 +00002258
2259
2260// Define these methods here so vtables don't get emitted into every translation
2261// unit that uses these classes.
2262
2263GetElementPtrInst *GetElementPtrInst::clone() const {
2264 return new GetElementPtrInst(*this);
2265}
2266
2267BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002268 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002269}
2270
Reid Spencerd9436b62006-11-20 01:22:35 +00002271CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002272 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002273}
2274
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2276AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2277FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2278LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2279StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2280CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2281CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2282CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2283CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2284CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2285CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2286CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2287CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2288CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2289CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2290CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2291CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2292CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2294VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2295
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002296ExtractElementInst *ExtractElementInst::clone() const {
2297 return new ExtractElementInst(*this);
2298}
2299InsertElementInst *InsertElementInst::clone() const {
2300 return new InsertElementInst(*this);
2301}
2302ShuffleVectorInst *ShuffleVectorInst::clone() const {
2303 return new ShuffleVectorInst(*this);
2304}
Chris Lattnerf22be932004-10-15 23:52:53 +00002305PHINode *PHINode::clone() const { return new PHINode(*this); }
2306ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2307BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2308SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2309InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2310UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002311UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}